实例成员不能用于类型 [英] Instance member cannot be used on type

查看:83
本文介绍了实例成员不能用于类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下课程:

class ReportView: NSView {  
    var categoriesPerPage = [[Int]]()
    var numPages: Int = { return categoriesPerPage.count }
}

编译失败,并显示以下消息:

Compilation fails with the message:

实例成员"categoriesPerPage"不能用于类型 "ReportView"

Instance member 'categoriesPerPage' cannot be used on type 'ReportView'

这是什么意思?

推荐答案

= {return self.someValue}时只是语法错误.不需要=.

You just have syntax error when saying = {return self.someValue}. The = isn't needed.

使用:

var numPages: Int {
    get{
        return categoriesPerPage.count
    }

}

如果您想只得到,可以写

var numPages: Int {
     return categoriesPerPage.count
}

使用第一种方法,您还可以将观察者添加为set willSet& didSet

with the first way you can also add observers as set willSet & didSet

var numPages: Int {
    get{
        return categoriesPerPage.count
    }
    set(v){
       self.categoriesPerPage = v
    }
}

允许使用= operator作为设置器

myObject.numPages = 5

这篇关于实例成员不能用于类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆