2016-12-07 70 views
-4

我有NSMutableDictionary與(鍵,值)pairs.For JournalId的關鍵是「journalId」。我只是想檢查我的字典是否包含特定的「journalId」,例如,29。如果存在,需要打印相應的標題和userId。 例如。條件檢查NSMutableDictionary

{ 
    journalId = 28; 
    title = Creed; 
    userId = 105; } { 
    journalId = 29; 
    title = Fg; 
    userId = 105; } { 
    journalId = 30; 
    title = Dhh; 
    userId = 105; } 

我想在詞典來檢查它是否具有journalId值= 28

if (dictValues["journalId"] == 28){ 
print(dictValues["title"] 
} 

上述方法示出了錯誤。 在此先感謝。

+5

什麼錯誤? – dasdom

+1

請提供完整的信息。 – itsdamslife

+0

你有字典數組嗎? –

回答

0

嘗試這種情況:

if let journalId = dictValues["journalId"] as? Int, journalId == 28 { 
    print(dictValues["title"]) 
} 
0

NSDictionary subsctiption返回AnyObject?類型的對象。 此代碼應解決您的問題

if (dictValues["journalId"] as? Int == 28) { 
    print(dictValues["title"]) 
} 
1
guard dictValues["journalId"] as! Int == 28 else { 
    print("key Not found") 
    return // exit the function 
} 
let title = dictValues["title"] as! String 
print("\(title)") 
+0

這個解決方案在swift中3.在Playground Xcode 8中執行。 – itsdamslife