call中的额外参数'error' - do / catch?

时间:2015-11-11 08:51:32

标签: swift

我知道有新的错误处理,即执行/捕获但不确定它是否适用于此,即使它确实如此,我甚至很难通过文档。有人可以告诉我正确的代码块。

     /*** error Extra argument 'error' in call ***/
    var plistDic = NSPropertyListSerialization.propertyListWithData(plistData!, 
    options:Int(NSPropertyListMutabilityOptions.MutableContainersAndLeaves.rawValue), 
    format: nil, error: &error) as Dictionary<String, Dictionary<String, String>> 

    assert(error == nil, "Can not read data from the plist")

    return plistDic
} 

   // END

编辑:

let YALCityName = "name"
let YALCityText = "text"
let YALCityPicture = "picture"

private let kCitiesSourcePlist = "Cities"

class YALCity: Equatable {

var name: String
var text: String
var image: UIImage
var identifier: String

// MARK: Class methods
class internal func defaultContent() -> Dictionary<String, Dictionary<String, String>> {

    let path = NSBundle.mainBundle().pathForResource(kCitiesSourcePlist, ofType: "plist")
    let plistData = NSData(contentsOfFile: path!)
    assert(plistData != nil, "Source doesn't exist")


    do {
        let plistDic = try NSPropertyListSerialization.propertyListWithData(plistData!,
            options:NSPropertyListMutabilityOptions.MutableContainersAndLeaves,
            format: nil
        )

        if let dictionary = plistDic as? Dictionary< String, Dictionary<String, String> > {

            print("\(dictionary)")

        }
        else {

            print("Houston we have a problem")
        }
    }
    catch let error as NSError {
        print(error)
    }


        return defaultContent()

}



init(record:CKRecord) {
    self.name = record.valueForKey(YALCityName) as! String
    self.text = record.valueForKey(YALCityText) as! String
    let imageData = record.valueForKey(YALCityPicture) as! NSData
    self.image = UIImage(data:imageData)!
    self.identifier = record.recordID.recordName
}

}

func ==(lhs: YALCity, rhs: YALCity) -> Bool {
return lhs.identifier == rhs.identifier
}

1 个答案:

答案 0 :(得分:1)

试试这段代码:

do {
    var plistDic = try NSPropertyListSerialization.propertyListWithData(plistData!,
         options:NSPropertyListMutabilityOptions.MutableContainersAndLeaves,   
         format: nil
         )

    // plistDic is of type 'AnyObject'. We need to cast it to the
    // appropriate dictionary type before using it.

    if let dictionary = plistDic as? Dictionary<String, Dictionary<String, String>> {
        // You are good to go.
        // Insert here your code that uses dictionary (otherwise
        // the compiler will complain about unused variables).
        // change 'let' for 'var' if you plan to modify the dictionary's
        // contents.

        // (...)
    }
    else {
        // Cast to dictionary failed: plistDic is NOT a Dictionary with
        // the structure: Dictionary<String, Dictionary<String, String>>
        // It is either a dictionary of a different internal structure,
        // or not a dictionary at all.  
    }
}
catch let error as NSError {
    // Deserialization failed (see console for details:)
    print(error)
}

注意:我将调用拆分为抛出(try...)的函数,并将投射分割为特定类型的Dictionaryif let...)因为我不确定究竟是什么如果调用成功但是转换失败,或者如果从调试器失败哪一个失败,则会发生。另外,我不喜欢在一行中发生太多事情......

编辑:我修复了options参数。在Swift中,Intenum不可互换;你需要传递正确的类型(我在修改你的代码时第一次错过了它)。