Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested method calls in objective c

Tags:

objective-c

Hey i have just started out with objective-c and i came across the nested messaging concept.I don't understand why we have to use and how it's used syntactically and semantically.

For example:

[[myAppObject theArray] insertObject:[myAppObject objectToInsert] atIndex:0]    

This was one i came across.My problem is that i don't know what [myAppObject theArray] is doing.Is it creating an instance of myAppObject or is there a class by that name with the method theArray?

Can anyone shed light light on this topic?

like image 994
JohnnyHunter Avatar asked Oct 24 '25 18:10

JohnnyHunter


2 Answers

That is an example of nested method calls. Simply:

[myAppObject theArray] is returning an array.

[myAppObject objectToInsert] is returning an object.

So:

[[myAppObject theArray] insertObject:[myAppObject objectToInsert] atIndex:0]

is the same as:

[an_array insertObject:an_object atIndex:0]

like image 71
Tyler Avatar answered Oct 26 '25 06:10

Tyler


Is it creating an instance of myAppObject or is there a class by that name with the method theArray

Neither; myAppObject is an instance of class MyAppObject (assuming conventional naming has been used) and the instance method, or property, theArray is being sent a message on that instance.

So MyAppObject would look something like this:

@interface MyAppObject : NSObject {
    NSArray *_theArray;   // This is optional, and considered to be old fashioned
                          // (but not by me).
}

@property (nonatomic, strong) NSArray *theArray;

...

@end

Which has been allocated like this, somewhere:

MyAppObject *myAppObject = [[MyAppObject alloc] init];
like image 35
trojanfoe Avatar answered Oct 26 '25 06:10

trojanfoe