实例成员不能用于类型|关闭

时间:2016-01-08 16:38:55

标签: swift cocoa

我有一个非常简单的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'

    }


}

1 个答案:

答案 0 :(得分:2)

初始化属性时,您无法访问实例范围,只能访问静态上下文。

试试这个:

var currentWeatherCallback: Weather? -> Void { return
    { (weather: Weather?) in
        self.currentTemp.stringValue = weather?.updatedTime
    }
}