Search code examples
iosobjective-c-category

Difference between self and [self class] in Category?


Look at the comments in my code, what is the difference between those two methods?

And why can't I use:

[self userDefaultsRead:key];

while I can use class methods this way:

[self persistenceKey:key];

Here is my code: enter image description here


Solution

  • self is an instance of the object. You cannot call any class method (methods declared with a +) that way.

    [self class] returns the class object, which is the correct way to call the method. This is the correct code, which is why it works.

    I recommend you go back to the basics and read this book cover to cover. Anything you don't understand, research it until you do:

    https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ProgrammingWithObjectiveC.pdf