2015-11-11 55 views
1

我知道有新的錯誤處理,即do/catch,但不知道它是否適用於此,即使這樣做對我來說,即使通過文檔也很困難。請有人給我看看正確的代碼塊。呼叫中的額外參數「錯誤」 - do/catch?

 /*** 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 
} 
+0

看起來這個方法的swift版本被定義爲'throws',並且拋棄了'error'參數。這是Swift 2中可可API的一個非常常見的模式,有很多關於這方面的信息(由Apple的文檔開始)。 –

回答

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...),因爲我不確定如果調用成功但鑄造失敗會發生什麼情況,或者如果能清楚哪一個失敗ebugger。另外,我不喜歡在一條線上發生太多事情......

編輯:我修正了options參數。在Swift中,Intenum不可互換;你需要傳遞正確的類型(我在修改代碼時第一次錯過了它)。

+0

就是您調用NSPropertyListSerialization.propertyListWithData()的地方。 –

+0

看我的編輯。我在我的環境(Xcode 7.1)中嘗試了新代碼並編譯。 –

+0

這是因爲你沒有使用'字典'(我的代碼說「你很好去」,假設你的代碼使用該變量)。你應該把你的邏輯(你在'dictionary'上操作的地方)放在那裏。查看我的最新修改 –