2017-10-28 111 views
0

我不得不嘗試下面的代碼轉換爲如何將下面的Objective-C中的JSON轉換爲Swift?

Objective-C的

NSArray *jsonObject = [NSJSONSerialization JSONObjectWithData:[json dataUsingEncoding:NSUTF8StringEncoding] 

斯威夫特

let json = JSONSerialization.jsonObject(with: json?.data(using: String.Encoding(rawValue: String.Encoding.utf8.rawValue)), options: JSONSerialization.ReadingOptions) 

,但我不斷收到此錯誤

不能援引 '的JSONObject'使用參數列表類型'(使用: Data ?,選項:JSONSeri alization.ReadingOptions.Type)'

回答

0

您應該利用帶有可選值的Swift。嘗試這個。

if let data = json?.data(using: .utf8), 
    let jsonObject = try? JSONSerialization.jsonObject(with: data, options: []) { 
    //jsonObject 
} 
相關問題