2016-04-10 4 views
0

UIAlertControllerにエラーメッセージを表示する可能性があるのだろうかと思います。swift show UIAlertControllerのエラーメッセージの配列

私のサーバーはJSONとしてエラーメッセージを返します。

私が使用して各エラーメッセージを取得することができます。

if let errorVal = errorVal { 

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


} 

今、私は私がAlertControllerでエラーを表示することができますか疑問。 AlertControllerのメッセージパラメータは、文字列を期待していますが、私のエラーは、JSONはその後、さて、あなたはそれぞれのエラー(または単にメッセージ)の説明を文字列を構築することができ

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) 

答えて

1

を.ARRAYにキャストとして来て、(があり得ることを示して表示するにはあまりにも多く)。

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 
     } 
    } 
} 

以降あなたのような何かを行うことができます上:

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) 
それはこのように行くだろう