问题描述
我怎样才能在斯威夫特的所有国家名称的数组?
我试着转换code我不得不在Objective-C,这是这样的:
How can I get an array with all countries names in Swift?I've tried to convert the code I had in Objective-C, which was this:
if (!pickerCountriesIsShown) {
NSMutableArray *countries = [NSMutableArray arrayWithCapacity: [[NSLocale ISOCountryCodes] count]];
for (NSString *countryCode in [NSLocale ISOCountryCodes])
{
NSString *identifier = [NSLocale localeIdentifierFromComponents: [NSDictionary dictionaryWithObject: countryCode forKey: NSLocaleCountryCode]];
NSString *country = [[NSLocale currentLocale] displayNameForKey: NSLocaleIdentifier value: identifier];
[countries addObject: country];
}
和斯威夫特我不能从这里经过:
And in Swift I can't pass from here:
if (!countriesPickerShown) {
var countries: NSMutableArray = NSMutableArray()
countries = NSMutableArray.arrayWithCapacity((NSLocale.ISOCountryCodes).count) // Here gives the Error. It marks NSLocale.ISOCountryCodes and .count
与你的人知道这件事吗?
Does anyone of you know about this?
感谢
推荐答案
首先 ISOCountry codeS
需要参数括号这样反而这将是 ISOCountry codeS()
。其次,你不需要围绕 NSLocale
和 ISOCountry codeS()
括号。此外,arrayWithCapacity是pcated这意味着它从语言除去德$ P $。这方面的一个工作版本会有点像这样
First of all ISOCountryCodes
requires argument parenthesis so instead it would be ISOCountryCodes()
. Second, you dont need parenthesis around NSLocale
and ISOCountryCodes()
. Also, arrayWithCapacity is deprecated meaning it is removed from the language. A working version of this would be somewhat like this
if (!countriesPickerShown) {
var countries = NSMutableArray()
countries = NSMutableArray(capacity: (NSLocale.ISOCountryCodes().count))
}
这篇关于斯威夫特 - 获取国家名单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!