My Blog List

Wednesday, June 1, 2011

Study Notes: Back to Syntax - [, ] and self



Click on the illustration above to fully enjoy the detail
About a month ago I was making connections between Objective-C and C++
Then I went on vacation for a few weeks
Time to rebuild the dendrites and remember what does what and who is who


* [ and ]  is all over the place in the Foundation and apps coding


One explanation I like (and it took me an hour last night to find it!) is on page 153 of Goldstei's Objective C for Dummies (I dislike the name of this series but the books usually work for me)


Quoting Goldstein here
[receiver message : arguments];


The receiver of a message can be either an object or a class.... (in) Objective-C you can send a message to a class..... (unlike C++) .... Class methods enable you to implement behavior that is not object-specific, but applicable to an entire class.


* The 'self' keyword is another item that I run across a lot in the Foundation template code. Sometimes it makes sense to me, and other times there's something about it that bothers me. Here's what Goldstein says about 'self' on page 269 of the same book


if (self = [super initWithAmount:theAmount forBudget:aBudget])


Here, Goldstein explains, we are "... assigning what (we) get back from (the) superclass's init method to self..... self is the hiden variable accessible to methods in an object that points to its instance variables.


Back on page 155 Goldstein explained the concept of self in greater detail.


"... how does (the code for a method) get to the object's ivars, which are sitting in some place in memory..... When you send a message to Objective-C, a hidden argument called self, a pointer to the object's instance variables, is passed to the receiving object.


example
[europeBudge spendDollars:numberDollarsInEuroland]


the method passes europeBudget as its self argument


..... As you create objects, you get a new pointer for each one, and when you send a message to a particular object, the pointer associated with that object becomes the self argument.


* Of course I still have to pickup self.


Quoting Goldstein on page 271


...the self = stateent ensures that self is set to whatever object I get back from the superclass initializer. After the code blocks that initialize the variables...


return self; 


* Furthermore the use of the self keyword, pageg 312/313 where Goldstein in a section titled "Accessing the instance variables from within the class"

".. you can access them from other objects or from main..... (how to access properties) from within the object walls.


[self setCountry:theCountry];


You can also use the dot notation similar to other object-oriented languages)


self.country = theCountry;


* So let's think about how to interpret the following code snip generated by the Framework for an appDelegate.m file


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {   
   
    // Override point for customization after app launch.
    [self.window addSubview:viewController.view];
    [self.window makeKeyAndVisible];


    return YES;
}











No comments:

Post a Comment