实例成员不能用于类型

时间:2015-09-02 10:59:48

标签: swift instantiation getter-setter computed-properties

我有以下课程:

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

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

  

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

这是什么意思?

8 个答案:

答案 0 :(得分:111)

= {return self.someValue}时,您只有语法错误。不需要=

使用:

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

}

如果你想只获得,你可以写

var numPages: Int {
     return categoriesPerPage.count
}

通过第一种方式,您还可以将观察者添加为set willSet& didSet

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

允许使用= operator作为setter

myObject.numPages = 5;

答案 1 :(得分:41)

对于其他任何偶然发现此事的人,请确保您不会尝试修改类而不是实例! (除非您已将变量声明为静态)

例如。

MyClass.variable = 'Foo' // WRONG! - Instance member 'variable' cannot be used on type 'MyClass'

instanceOfMyClass.variable = 'Foo' // Right!

答案 2 :(得分:35)

有时Xcode会在覆盖方法时添加class func,而不仅仅是func。然后在静态方法中看不到实例属性。忽略它很容易。那是我的情况。

enter image description here

答案 3 :(得分:13)

它说你有一个实例变量(只有当你有一个该类的实例时,var才可见/可访问)并且你试图在静态范围的上下文中使用它(类方法)。

您可以通过添加静态属性使实例变量成为类变量。

您可以通过删除class属性使您的类方法成为实例方法。

答案 4 :(得分:6)

另一个例子是,你有类似的课程:

@obc class Album: NSObject {
    let name:String
    let singer:Singer
    let artwork:URL
    let playingSong:Song


    // ...

    class func getCurrentlyPlayingSongLyric(duration: Int = 0) -> String {
        // ...
       return playingSong.lyric
    }
}

你也会得到相同类型的错误:

instance member x cannot be used on type x. 

这是因为您为方法分配了" class" keyword(使您的方法成为类型方法)并使用like:

Album.getCurrentlyPlayingSongLyric(duration: 5)

但之前谁设置了playSong变量?好。在这种情况下你不应该使用class关键字:

 // ...

 func getCurrentlyPlayingSongLyric(duration: Int = 0) -> String {
        // ...
       return playingSong.lyric
 }

 // ...

现在你可以自由地去了。

答案 5 :(得分:1)

您最初的问题是:

Ref::map
  

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

以前的帖子正确指出,如果您要使用计算的属性,则class ReportView: NSView { var categoriesPerPage = [[Int]]() var numPages: Int = { return categoriesPerPage.count } } 标记是错误的。

错误的其他可能性:

如果您要进行“使用闭包或函数设置默认属性值” ,则只需稍作更改即可。 (注意:此示例显然不打算这样做)

=

我们添加class ReportView: NSView { var categoriesPerPage = [[Int]]() var numPages: Int = { return categoriesPerPage.count }() } 来表示默认的初始化闭包,而不是删除=。 (这在初始化UI代码时很有用,以将其全部放在一个位置。)

但是,发生 完全相同的错误

  

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

问题是试图用另一个属性的值初始化一个属性。一种解决方案是制作初始化器()。除非有人访问该值,否则它将不会执行。

lazy

现在编译器很高兴!

答案 6 :(得分:0)

尽管变量static,但我仍然遇到同样的错误。 解决方案:清理构建,清理派生数据,重新启动Xcode。或捷径 Cmd + Shift + Alt + K

UserNotificationCenterWrapper.delegate = self

public static var delegate: UNUserNotificationCenterDelegate? {
        get {
            return UNUserNotificationCenter.current().delegate
        }
        set {
            UNUserNotificationCenter.current().delegate = newValue
        }
    }

答案 7 :(得分:0)

只要有人真的需要这样的关闭,就可以通过以下方式完成:

var categoriesPerPage = [[Int]]()
var numPagesClosure: ()->Int {
    return {
        return self.categoriesPerPage.count
    }
}
相关问题