Wednesday, 11 September 2013

How do foundation classes know what instance to act upon when an instance method is called?

How do foundation classes know what instance to act upon when an instance
method is called?

Certain foundation classes such as NSString or NSArray have instance
methods that do something with that instance, but never ask for the
instance to be given to then. For example:
NSArray *array = @[@"hi"];
Int number = [array count];
The count method knows which array to count without asking for one to be
provided. How so you do that? I'm looking to make a category on NSString
with an incrementByOne instance method. I don't want to ask for the string
in the method deceleration, I want to know what object I'm performing the
action on, just like count knows which array to work on. If anyone can
help me out, I would greatly appreciate it.

No comments:

Post a Comment