arowmy初始化工作正常斯威夫特<2,但是在斯威夫特2我从Xcode中的错误消息Call can throw, but it is not marked with 'try' and the error is not handled的let anyObj = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [String:AnyObject].我认为在我的情况下我不能使用try catch块,因为此时super尚未初始化."尝试"需要一个抛出的功能.
这是我的功能:
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)
}
}
Run Code Online (Sandbox Code Playgroud)
Xcode意味着:
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
Rob*_*Rob 64
该jsonObject罐throw的错误,所以把它内do块,使用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)")
}
Run Code Online (Sandbox Code Playgroud)
或者,在Swift 4中,您可以通过使struct符合以下内容来简化代码Codable:
struct Fieldpropertie: Codable {
let label: String
let value: Int
let uprate: Int
let suffix: String
}
Run Code Online (Sandbox Code Playgroud)
然后
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)")
}
Run Code Online (Sandbox Code Playgroud)
对于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)")
}
Run Code Online (Sandbox Code Playgroud)
errorin 的类型catch let error是ErrorType