PDA

View Full Version : Self help




larswik
Sep 4, 2010, 02:51 PM
Can someone point me too any tutorials on the 'Self'? The book Learning obj C 2.0 uses an example of self by reducing down fractions. I hated fraction in high school and I hate them even more now :) So it makes it hard for to get the concept of Self and how to use it.

If Steve writes another book I hope he gets rid of his fraction examples. My python book tought you programing through games and made it fun. Learning programming is difficult enough make it more fun :)



lloyddean
Sep 4, 2010, 03:03 PM
Fractions are important and necessary in order to minimize the effects of rounding errors. So, if I were you I'd, get used to them.

Cromulent
Sep 4, 2010, 03:05 PM
The self keyword is simply a pointer for the current instance of a class. For instance say you declare a class and want to provide a designated initialiser, the init method would call the designated initialiser whilst the second initialiser would do the initialisation.

Something along these lines. Notice the use of the self keyword to call the initWithPath: method in the same instance of the class.

Also you state you know how to use Python, well the self keyword in Objective-C is the same as the self keyword in Python in concept.

- (id) init
{
return [self initWithPath: nil];
}

- (id) initWithPath: (NSString *) path
{
self = [super init]
if(self)
{
// perform initialisation here
return self;
}
return nil;
}

lee1210
Sep 4, 2010, 03:23 PM
If nuts and bolts help, look at "Using Hidden Arguments" here:
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtHowMessagingWorks.html#//apple_ref/doc/uid/TP40008048-CH104-SW1

-Lee

larswik
Sep 5, 2010, 03:52 PM
Cromulent --- Thanks for the example. The example is still a little advanced for where I am on the learning curve. Next chapter I get into 'init' it looks like and 'id'.

Lee- Thanks for the nuts and bolts. I may have to go back to the fraction class in the book. The web seems to be scares with 'simple' examples. Most of the examples out there are using examples of code that I have not learned yet which makes it hard to understand.

Today's goal is to learn SELF and it seems that this SUPER INIT always comes a long with that code too.

-Lars

Matthew Yohe
Sep 5, 2010, 03:59 PM
Every Cocoa/Cocoa Touch object inherits from NSObject (http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html).

Check out the adopted protocols there. Those methods can be called on any object because of their inheritance of NSObject.

Also, you should probably learn how fractions work.