在Swift(2.0)中正确处理NSJSONSerialization(try catch)? [英] Correct handling of NSJSONSerialization (try catch) in Swift (2.0)?
问题描述
arowmy init在Swift< 2但是在Swift 2中我收到来自Xcode的错误消息 Call可以抛出,但是没有标记为'try'并且错误未被处理
at 让anyObj = NSJSONSerialization.JSONObjectWithData(数据,选项:NSJSONReadingOptions.MutableContainers)为! [字符串:AnyObject]
。我认为在我的情况下我不能使用try catch块,因为此时不会初始化super。 尝试需要一个抛出的功能。
arowmy init works fine in Swift < 2 but in Swift 2 I get a error message from Xcode Call can throw, but it is not marked with 'try' and the error is not handled
at let anyObj = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as! [String:AnyObject]
. I think in my case I can´t use a try catch block because super is not initialized at this time. "Try" need a function that throws.
这是我的功能:
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意味着:
让anyObj =尝试NSJSONSerialization .JSONObjectWithData(数据,选项:NSJSONReadingOptions.MutableContainers)为! [String:AnyObject]
Xcode mean that:
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
but I have no idea to do here the right think according to this document https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ErrorHandling.html
推荐答案
jsonObject
可以抛出
错误,所以把它放在 do
块中,使用试试
, catch
抛出任何错误。在Swift 3中:
The jsonObject
can throw
errors, so put it within do
block, use try
, and catch
any errors thrown. In 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,请参阅此答案的上一版本。
For Swift 2, see previous revision of this answer.
这篇关于在Swift(2.0)中正确处理NSJSONSerialization(try catch)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!