2017-01-27 56 views
0

我試圖與PropertyListSerialization字典中迅速2,但是不斷地給出了這樣的錯誤:使用PropertyListSerialization使一個詞典中迅速2

"can not convert value of type 'Int' to expected argument type 'NSPropertyListReadOptions' 

我的代碼行是:

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {response,data,error in 
      if data != nil { 
      let datasourceDictionary = try! NSPropertyListSerialization.propertyListWithData(data! ,options:Int(NSPropertyListMutabilityOptions.Immutable.rawValue), format: nil) 

這:

let datasourceDictionary = try! NSPropertyListSerialization.propertyListWithData(data! ,options:Int(NSPropertyListMutabilityOptions.Immutable.rawValue), format: nil) 

之後,我不得不遍歷所有的字典:

for(key, value): (AnyObject, AnyObject) in datasourceDictionary { 
      let name = key as? String 
      let url = NSURL(string:value as? String ?? "") 
      if name != nil && url != nil { 
      let photoRecord = PhotoRecord(name:name!, url:url!) 
      self.photos.append(photoRecord) 
      } 
     } 

回答

3

你應該解開,而不是你的數據對象檢查它反對零的。你也需要實現會盡力捕捉錯誤處理,而不是強迫它:

if let data = data { 
    do { 
     let datasourceDictionary = try NSPropertyListSerialization.propertyListWithData(data, options: .MutableContainersAndLeaves, format: nil) 
     print(datasourceDictionary) 
    } catch let error as NSError { 
     print(error.code, error.domain) 
    } 
} 
+0

福氣 - 給予好評 – muescha

+0

{ 「大塊頭」 =「http://www.sxc.hu/pic/m/n/納克/ ngould/325616_big_fella.jpg「; 「Camino al volcan」=「http://www.sxc.hu/pic/m/r/ro/rockberto/489892_camino_al_volcan_.jpg」; Cheetah =「http://www.sxc.hu/pic/m/n/ng/ngould/329660_cheetah_1.jpg」; } 得到這個。 –

0

你不需要將其更改爲INT:

NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()) {response,data,error in 
      if data != nil { 
      let datasourceDictionary = try! NSPropertyListSerialization 
              .propertyListWithData(data!, 
               options:NSPropertyListMutabilityOptions.Immutable, 
               format: nil) 

這:

let datasourceDictionary = try! NSPropertyListSerialization 
              .propertyListWithData(data! , 
               options:NSPropertyListMutabilityOptions.Immutable, 
               format: nil)