【发布时间】:2015-09-04 18:06:07
【问题描述】:
- 我编写了一个将非可选字符串作为参数的函数。
- 我声明了一个 String 类型的变量属性,它也不是可选的。
- 当我尝试将此属性作为参数调用我的函数时,出现以下错误。
无法使用“(字符串)”类型的参数列表调用“localesForCountryCode”
请注意,错误将类型列为'(String)' 而不是'String'。 括号是什么意思?我认为他们的意思是类型是可选的,但没有任何地方声明为可选。
我的函数(NSLocale 的扩展):
func localesForCountryCode(countryCode: String) -> [NSLocale] {
let localeIdentifiers = localeIdentifiersForCountryCode(countryCode)
var locales = [NSLocale]()
for localeIdentifier in localeIdentifiers {
let localeForIdentifier = NSLocale(localeIdentifier: localeIdentifier)
locales.append(localeForIdentifier)
}
return locales
}
调用我的函数的代码
let currentCountryCode = "US"
var localesForCurrentCountry = [NSLocale]()
func updateWithNewLocation(newLocation: CLLocation) {
geoCoder.reverseGeocodeLocation(newLocation, completionHandler: { (placemarks, error) -> Void in
if placemarks.count > 0 {
let placemark = placemarks.first as! CLPlacemark
self.currentCountry = placemark.country
self.localesForCurrentCountry = NSLocale.localesForCountryCode(self.currentCountryCode)
}
})
}
更新 1
当我将函数代码从 NSLocale 扩展移动到我从中调用函数的视图控制器时,错误消失了。 知道为什么会出现这种情况吗?绝对没有对函数进行任何更改。
更新 2
【问题讨论】:
-
你能显示使用代码吗?
-
括号只是表示方法的类型和参数列表。
-
rintaro 这是一个不同的问题。
-
我想你可能不得不把
func和class func联系起来,因为你通过NSLocale.引用它!
标签: ios xcode swift compiler-errors