Self help

Discussion in 'Mac Programming' started by larswik, Sep 4, 2010.

  1. larswik macrumors 68000

    Sep 8, 2006
    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 :)
  2. lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
    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.
  3. Cromulent macrumors 603


    Oct 2, 2006
    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.

    - (id) init
        return [self initWithPath: nil];
    - (id) initWithPath: (NSString *) path
        self = [super init]
            // perform initialisation here
            return self;
        return nil;
  4. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
  5. larswik thread starter macrumors 68000

    Sep 8, 2006
    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.

  6. Matthew Yohe macrumors 68020

    Oct 12, 2006
    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.

Share This Page