Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instance member cannot be used on type

I have the following class:

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

Compilation fails with the message:

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

What does this mean?

like image 707
Aderstedt Avatar asked Sep 02 '15 10:09

Aderstedt


2 Answers

Sometimes Xcode when overrides methods adds class func instead of just func. Then in static method you can't see instance properties. It is very easy to overlook it. That was my case.

enter image description here

like image 124
milczi Avatar answered Oct 02 '22 15:10

milczi


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

Use :

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

}

if you want get only you can write

var numPages: Int {
     return categoriesPerPage.count
}

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
    }
}

allowing to use = operator as a setter

myObject.numPages = 5
like image 44
Daniel Krom Avatar answered Oct 02 '22 17:10

Daniel Krom