swift字典返回零

swift字典返回零,swift,swift2,Swift,Swift2,打印函数永远不会执行。但是,如果我删除iflet,我可以打印countryCode,但不能打印aValue,它将始终返回nil。 如何修复此问题?您正在混合密钥和对象,请改为: let countryCodes = NSLocale.ISOCountryCodes() for countryCode in countryCodes { let dictionary = NSDictionary(object: countryCode, forKey: NSLocaleCountryCo

打印函数永远不会执行。但是,如果我删除if
let
,我可以打印
countryCode
,但不能打印
aValue
,它将始终返回
nil

如何修复此问题?

您正在混合密钥和对象,请改为:

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 aValue = dictionary[NSLocaleCountryCode] {

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