2015-06-22 44 views
0

下面的代碼給我的錯誤「'AnyObject'不能轉換爲'字符串'」在我把我的「if let」語句解開的行從Parse中提取的可選productData。我只是想從Parse中的對象中拉出一個String。我已到處檢查,所有標準答案/解決方案都無法正常工作。有什麼想法嗎?'AnyObject'不能轉換爲'字符串',而解包可選

大部分代碼是直接取自解析iOS的文檔Here

import Foundation 

import Parse 

func getDataFromParse() { 

var productDataFromParse = PFQuery(className:"Product") 

productDataFromParse.getObjectInBackgroundWithId("uyeXHufDgq") { 

(productData: PFObject?, error: NSError?) -> Void in 

if error == nil && productData != nil { 

    if let productTitle = productData["productTitle"] as! String { 
     self.productTitle = productTitle 
    } 


} else { 
    println(error) 
} 
} 

} 
+0

是你的問題解決了還是你有更多的問題?如果有,請選擇其中一個答案。如果沒有,請告訴我,我會盡力幫忙。 –

回答

0

productData: PFObject?對象是一個可選的本身。你還不能對它進行下標,因爲它之前需要解開。您可以用可選鏈替代​​檢查。

在雨燕1.2,你可以做到這一點,你的錯誤在同一個語句檢查:

if let productTitle = productData?["productTitle"] as? String 
    where error == nil { 
    self.productTitle = productTitle 
} 

productData和方括號之間的附加?