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?
68 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.
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
3For anyone else who stumbles on this make sure you're not attempting to modify the class rather than the instance! (unless you've declared the variable as static)
eg.
MyClass.variable = 'Foo' // WRONG! - Instance member 'variable' cannot be used on type 'MyClass' instanceOfMyClass.variable = 'Foo' // Right!
2It is saying you have an instance variable (the var is only visible/accessible when you have an instance of that class) and you are trying to use it in the context of a static scope (class method).
You can make your instance variable a class variable by adding static/class attribute.
You instantiate an instance of your class and call the instance method on that variable.
1Another example is, you have class like :
@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 } }
you will also get the same type of error like :
instance member x cannot be used on type x.
It's because you assign your method with "class" keyword (which makes your method a type method) and using like :
Album.getCurrentlyPlayingSongLyric(duration: 5)
but who set the playingSong variable before? Ok. You shouldn't use class keyword for that case :
// ... func getCurrentlyPlayingSongLyric(duration: Int = 0) -> String { // ... return playingSong.lyric } // ...
Now you're free to go.
Your initial problem was:
class ReportView: NSView { var categoriesPerPage = [[Int]]() var numPages: Int = { return categoriesPerPage.count } }
Instance member 'categoriesPerPage' cannot be used on type 'ReportView'
previous posts correctly point out, if you want a computed property, the =
sign is errant.
Additional possibility for error:
If your intent was to "Setting a Default Property Value with a Closure or Function", you need only slightly change it as well. (Note: this example was obviously not intended to do that)
class ReportView: NSView { var categoriesPerPage = [[Int]]() var numPages: Int = { return categoriesPerPage.count }() }
Instead of removing the =
, we add ()
to denote a default initialization closure. (This can be useful when initializing UI code, to keep it all in one place.)
However, the exact same error occurs:
Instance member 'categoriesPerPage' cannot be used on type 'ReportView'
The problem is trying to initialize one property with the value of another. One solution is to make the initializer lazy
. It will not be executed until the value is accessed.
class ReportView: NSView { var categoriesPerPage = [[Int]]() lazy var numPages: Int = { return categoriesPerPage.count }() }
now the compiler is happy!
I kept getting the same error inspite of making the variable static
. Solution: Clean Build, Clean Derived Data, Restart Xcode. Or shortcut Cmd + Shift+Alt+K
UserNotificationCenterWrapper.delegate = self public static var delegate: UNUserNotificationCenterDelegate? { get { return UNUserNotificationCenter.current().delegate } set { UNUserNotificationCenter.current().delegate = newValue } }
Just in case someone really needs a closure like that, it can be done in the following way:
var categoriesPerPage = [[Int]]() var numPagesClosure: ()->Int { return { return self.categoriesPerPage.count } }
ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoamtrZWaAdX%2BOoqWsrJGjsKZ5zJ6km52iYrCius2oq2aalWLCtLHDZqanZaSuvaY%3D