在Swift(2.0)中正确处理NSJSONSerialization(try catch)?

arowmy init在Swift 2中工作正常,但是在Swift 2中,我从Xcode Call can throw, but it is not marked with 'try' and the error is not handled得到一个错误消息, Call can throw, but it is not marked with 'try' and the error is not handledCall can throw, but it is not marked with 'try' and the error is not handledlet anyObj = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [String:AnyObject] let anyObj = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [String:AnyObject] 。 我认为在我的情况下,我不能使用try catch块,因为超级目前还没有初始化。 “尝试”需要一个抛出的函数。

这里是我的function:

 required init(coder aDecoder : NSCoder) { self.name = String(stringInterpolationSegment: aDecoder.decodeObjectForKey("name") as! String!) self.number = Int(aDecoder.decodeIntegerForKey("number")) self.img = String(stringInterpolationSegment: aDecoder.decodeObjectForKey("image") as! String!) self.fieldproperties = [] var tmpArray = [String]() tmpArray = aDecoder.decodeObjectForKey("properties") as! [String] let c : Int = tmpArray.count for var i = 0; i < c; i++ { let data : NSData = tmpArray[i].dataUsingEncoding(NSUTF8StringEncoding)! // Xcode(7) give me error: 'CAll can thorw, but it is not marked with 'try' and the error is not handled' let anyObj = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [String:AnyObject] let label = anyObj["label"] as AnyObject! as! String let value = anyObj["value"] as AnyObject! as! Int let uprate = anyObj["uprate"] as AnyObject! as! Int let sufix = anyObj["sufix"] as AnyObject! as! String let props = Fieldpropertie(label: label, value: value, uprate: uprate, sufix: sufix) self.fieldproperties.append(props) } } 

Xcode的意思是: let anyObj = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [String:AnyObject] let anyObj = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [String:AnyObject]

但是我不知道在这里按照这个文档做这个想法https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html

jsonObject可以throw错误,所以把它放在块内,使用try ,并catch抛出的任何错误。 在Swift 3:

 do { let anyObj = try JSONSerialization.jsonObject(with: data) as! [String: Any] let label = anyObj["label"] as! String let value = anyObj["value"] as! Int let uprate = anyObj["uprate"] as! Int let sufix = anyObj["sufix"] as! String let props = Fieldpropertie(label: label, value: value, uprate: uprate, sufix: sufix) // etc. } catch { print("json error: \(error.localizedDescription)") } 

或者,在Swift 4中,您可以通过使您的struct符合Codable来简化您的代码:

 struct Fieldpropertie: Codable { let label: String let value: Int let uprate: Int let suffix: String } 

然后

 do { let props = try JSONDecoder().decode(Fieldpropertie.self, from: data) // use props here; no manual parsing the properties is needed } catch { print("json error: \(error.localizedDescription)") } 

对于Swift 2,请参阅此答案的以前的修订 。

JSONSerialization.JSONObject抛出ErrorType而不是NSError。

所以正确的抓住是

 do { let anyObj = try JSONSerialization.JSONObject(with: data, options: []) as! [String:AnyObject] // use anyObj here } catch let error { print("json error: \(error)") } 

catch let errorerrortypescatch let errorErrorType

不知道它是否会解决你的问题,但不是方法JSONObjectWithData:options:error: 我想你错过了error参数。