Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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 :)
 

Cromulent

macrumors 604
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
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.

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

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

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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

macrumors 68020
Oct 12, 2006
2,200
142
Every Cocoa/Cocoa Touch object inherits from NSObject.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.