2016-04-10 20 views
0

hata iletilerinde bir dizi hata mesajı gösterimi UIAlertController'da hata iletilerinin gösterilip gösterilmeyeceğini merak ediyorum.UIAlertController

Sunucum hata iletileri JSON olarak geri gönderiyor.

Ben kullanarak her hata mesajı alabilirsiniz:

if let errorVal = errorVal { 

    if let items = errorVal["errors"].array { 
     for item in items { 
      print(item) 
     } 
    } 


} 

Şimdi ben bir AlertController hatalar gösterebilir merak ediyorum. AlertController mesajı parametresi bir dize bekliyoruz ama benim hataları JSON olarak gelip sonra, her hatanın açıklaması (veya yalnızca mesajın) ile bir dize kadar inşa edebileceğini

let alertController = UIAlertController(title: "Hey! :)", message: "My Errors", preferredStyle: .Alert) 

let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 

alertController.addAction(defaultAction) 

self.presentViewController(alertController, animated: true, completion: nil) 

cevap

1

Peki .array için döküm ve (olabileceğini göstermektedir gösterecek çok fazla).

var errorMessages = "" 
if let errorVal = errorVal { 
    if let items = errorVal["errors"].array { 
     for item in items { 
      print(item) 
      errorMessages = errorMessages + item + "\n" // if this is NSError you can use description, message or code 
     } 
    } 
} 

ve daha sonra size böyle bir şey yapabilirsiniz: Böyle gider

let alertController = UIAlertController(title: "Hey! :)", message: errorMessages , preferredStyle: .Alert) 

let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil) 

alertController.addAction(defaultAction) 

self.presentViewController(alertController, animated: true, completion: nil) 
İlgili konular