实例成员不能在类型|上使用关闭 [英] Instance member cannot be used on type | Closures
问题描述
我有一个非常简单的 NSViewController
,它使用 WeatherFetcher
类命中一个API。它将完成处理程序作为参数传递,以便HTTP API在完成后可以调用它。
I have a very simple NSViewController
, which hits an API using a WeatherFetcher
class. It passes a completion handler as a parameter, so that the HTTP API can call it once it finishes.
在这个完成处理程序中,我试图为API的结果设置一个简单的 NSTextField
的值。但是,我得到这个错误:
实例成员'currentTemp'不能用于类型'WeatherView'
In this completion handler, I am trying to set the value of a simple NSTextField
to the result of API. However, I am getting this error:
Instance member 'currentTemp' cannot be used on type 'WeatherView'
任何想法是什么问题?
Any idea what is wrong?
class WeatherView: NSViewController {
var weather:Weather?
@IBOutlet weak var currentTemp: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
WeatherFetcher.fetchCurrent(currentWeatherCallback)
}
override var nibName : String{
return "WeatherView"
}
var currentWeatherCallback =
{
(weather: Weather?) in
currentTemp.stringValue = weather?.updatedTime
// Instance member 'currentTemp' cannot be used on type 'WeatherView'
}
}
推荐答案
初始化属性时,您无权访问实例作用域,只能访问静态上下文。
You don't have access to the instance scope when initialising properties, you can only access the static context.
试试这个:
var currentWeatherCallback: Weather? -> Void { return
{ (weather: Weather?) in
self.currentTemp.stringValue = weather?.updatedTime
}
}
这篇关于实例成员不能在类型|上使用关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!