2016-07-29 61 views
-2
let countryCodes = NSLocale.ISOCountryCodes() 

for countryCode in countryCodes { 
    let dictionary = NSDictionary(object: countryCode, forKey: NSLocaleCountryCode) 

    if let aValue = dictionary[countryCode] { 
     print("country code of \(countryCode) is \(aValue)") 
     } 

打印功能从未得到执行。但是,如果我删除了if let,我可以得到countryCode而不是aValue,它总是会返回nil。 我该如何解决这个问题?swift dictionary is returning nil

回答

3

你是混合键和对象,这样做,而不是:

if let aValue = dictionary[NSLocaleCountryCode] { 

      print("country code of \(countryCode) is \(aValue)") 
     } 
+1

哇,谢谢。我不习惯initObjectForKey字典。我想我的教训。 – Suhaib

相关问题