小编Mir*_*ner的帖子

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

arowmy初始化工作正常斯威夫特<2,但是在斯威夫特2我从Xcode中的错误消息Call 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].我认为在我的情况下我不能使用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 …
Run Code Online (Sandbox Code Playgroud)

ios swift

29
推荐指数
2
解决办法
3万
查看次数

标签 统计

ios ×1

swift ×1