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?
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]
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With