2015-03-18 65 views
0

我想從parse.com獲取數據並將數據添加到數組或字典中。我一直在收到錯誤,有誰知道解析數據的最佳解決方案。是否可以返回字典並在另一個函數中使用它?Swift的Parse.com數據查詢字典

致命錯誤:意外發現零而展開的可選值

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

是quoteText和quoteAuthor都是字符串列,並且兩列是否在每一行都包含非零值?如果沒有,你會插入一個零字符被聲明爲字符串值。順便說一下,使用PFObjects有什麼問題,爲什麼要將它們還原爲字典? – danh 2015-03-18 21:39:27

+0

是的,quoteText和quoteAuthor都是字符串列,它們都包含值。用PFObject可以給我一個例子。我已經看到並嘗試使用文檔中描述的許多方法,但我沒有工作。你不必幫助我,但如果你能給我幾個指示,那將是很棒的。 – 2015-03-18 21:47:33

+0

我很樂意提供更多幫助,但我並不十分清楚。然而,在objective-c應用程序中,我經常使用PFObjects,像解析返回一樣,將它們看作是詞典。他們響應allKeys和objectForKey:和setObject:forKey :.這通常是我需要的99%。 – danh 2015-03-18 21:49:56

回答

0

發揮它的安全,並使用默認值:

let quoteText = object["quoteText"] as? String ?? "" 
let quoteAuthor = object["quoteAuthor"] as? String ?? "" 

這種安全處理空值。