我正在尝试从parse.com获取数据并将数据添加到数组或字典中。我一直都会遇到错误,有人知道从解析中获取数据的最佳解决方案。是否可以返回字典并在另一个函数中使用它?
致命错误:在解包可选值时意外发现nil
func getQuote() {
var query = PFQuery(className: "Quote")
var quote = PFObject(className:"Quote")
var quoteDictionary: [String:String]!
query.findObjectsInBackgroundWithBlock { (objects: [AnyObject]!, error: NSError!) -> Void in
if error == nil {
if let objects = objects as? [PFObject] {
for object in objects {
var quoteText = object["quoteText"] as String
var quoteAuthor = object["quoteAuthor"] as String
quoteDictionary[quoteAuthor] = quoteText
println(quoteDictionary)
println(quoteAuthor)
println(quoteText)
}
}
} else {
println("Error")
}
}
}
答案 0 :(得分:0)
安全地播放并使用默认值:
let quoteText = object["quoteText"] as? String ?? ""
let quoteAuthor = object["quoteAuthor"] as? String ?? ""
这可以安全地处理空值。