Self help

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

  1. macrumors 68000

    Joined:
    Sep 8, 2006
    #1
    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. macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    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. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    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;
    }
     
  4. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
  5. thread starter macrumors 68000

    Joined:
    Sep 8, 2006
    #5
    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
     
  6. macrumors 68020

    Joined:
    Oct 12, 2006
    #6
    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