我有一个非常简单的NSViewController
,它使用WeatherFetcher
类命中API。它将完成处理程序作为参数传递,以便HTTP API在完成后可以调用它。
在此完成处理程序中,我尝试将简单NSTextField
的值设置为API的结果。但是,我收到此错误:
Instance member 'currentTemp' cannot be used on type 'WeatherView'
知道出了什么问题吗?
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'
}
}
答案 0 :(得分:2)
初始化属性时,您无法访问实例范围,只能访问静态上下文。
试试这个:
var currentWeatherCallback: Weather? -> Void { return
{ (weather: Weather?) in
self.currentTemp.stringValue = weather?.updatedTime
}
}