2016-07-29 12 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)") 
     } 

印刷機能は実行されません。ただし、letを削除した場合、countryCodeが出力されますが、aValueは出力されません。常にnilが返されます。 これを修正するにはどうすればよいですか?swift辞書がnilを返す

答えて

3

あなたがキーとオブジェクトを混合され、代わりにそれを行う:

if let aValue = dictionary[NSLocaleCountryCode] { 

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

うわー、ありがとう。私はinitObjectForKey辞書に慣れていません。私が推測する私の教訓を学んだ。 – Suhaib

関連する問題