Instance member cannot be used on type

后端 未结 8 1877
遥遥无期
遥遥无期 2020-11-29 04:13

I have the following class:

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


        
8条回答
  •  北海茫月
    2020-11-29 05:04

    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
    

提交回复
热议问题