[[self class] thumbnailSize] vs [self thumbnailSize]


#1

On page 287, line 7 from the top,

What is the point of doing “[[self class] thumbnailSize]” when invoking the class method instead of “[self thumbnailSize]”?

Maybe I am missing something here. Could someone enlighten me?

Thank you.


#2

self is an instance of a class, [self class] is the class itself. The alternative to [[self class] thumbnailSize] would be [Possession thumbnailSize]. The reason you use [self class] instead of Possession is to future-proof your classes in case they get subclassed. If you subclassed Possession with MySpecialPossesion, you’d want [MySpecialPossession thumbnailSize] and not [Possession thumbnailSize]. Doing [self class] checks the class at runtime, thus allows this call to be flexible.


#3

Thank you for the explanation.

But, can it not be [self thumbnailSize] instead of [Possession thumbnailSize]?

Do you have to use [self class] instead of ‘self’ as in [self methodName] to call the class method?

Thank you.


#4

You cannot use self to call a class method. self is an instance of the class, and will only respond to instance methods (methods that are prefixed with the - (minus) instead). Possession or [self class] are both class objects, so they will respond to class methods (methods that are prefixed with the + (plus)).


#5

You hit the nail on the head. Now I clearly understand.

Thank you very much.