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

Die Druckfunktion wird nie ausgeführt. Jedoch, wenn ich die if let entferne, kann ich countryCode gedruckt bekommen, aber nicht aValue, es wird immer nil zurückgeben. Wie behebe ich das?swift dictionary gibt null zurück

Antwort

3

Ihr Schlüssel und Objekt mischen, tun, dass statt:

if let aValue = dictionary[NSLocaleCountryCode] { 

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

wow, danke. Ich bin nicht an initObjectForKey-Wörterbücher gewöhnt. Ich habe meine Lektion gelernt. – Suhaib