2017-04-30 67 views
0

需要一些非常基本的幫助。Im新到swift並找不到任何好的解決方案。 我知道它非常基本的基礎,但是JSON和Swift更難,於是我想: 「)Alamofire解析和concat JSON在swift中

Alamofire.request("https://website.com/foobar").responseJSON { response in  

    let title = "" 
    var message = "" 

    if let result = response.result.value { 
     let JSON = result as! NSDictionary 
     print(JSON) // prints correctly the json 

     // { 
     //  id: 29, 
     //  title: "Foobar", 
     //  email: "[email protected]", 
     //  city: "Berlin", 
     //  name: "John Doe", 
     //  consumer_iban: null, 
     //  updated_at: "2017-04-18 23:47:44" 
     // } 

     for(key,value) in JSON{ 
      print("key \(key) value2 \(value)") 
      // this is correct shown too 
     } 
    } 

但我想是這樣的:

if(key == "title") { 
    title = value; 
} 
else { 
    message += (\(key): \(value)) 
} 

但是concatting並指的作爲重點串doensn't工作。

回答

0

你可以做

let title = JSON["title"] 
0

首先,不要在使用雨燕NSDictionary,你扔掉的類型信息

let JSON = result as! [String:Any] 

的問題是缺少雙引號,你需要那些字符串插值。

if key == "title" { 
    title = value 
} 
else { 
    message += "\(key): \(value)" 
} 

BTW:圍繞if條件括號和分號不需要在斯威夫特