what is self? when should i use it?

5 Solutions Collect From Internet About “what is self? when should i use it?”

self refers to the instance of the current class that you are working in, and yes, it is analagous to this in Java.

You use it if you want to perform an operation on the current instance of that class. For example, if you are writing an instance method on a class, and you want to call a method on that same instance to do something or retrieve some data, you would use self:

int value = [self returnSomeInteger];

This is also often used for accessor methods on an instance (i.e. setters and getters) especially with setter methods, if they implement extra functionality rather than just setting the value of an instance variable, so that you do not have to repeat that code over and over when you want to set the value of that variable, for example:

[self setSomeVariable:newValue];

One of the most common uses of self is during initialization of a class. Sample code might look like:

- (id)init
{
    self = [super init];

    if(self!=nil) {
        //Do stuff, such as initializing instance variables
    }

    return self;
}

This invokes the superclass’s (via super) initializer, which is how chained initialization occurs up the class hierarchy. The returned value is then set to self, however, because the superclass’s initializer could return a different object than the superclass.

self is an implied argument to all Obj-C methods that contains a pointer to the current object in instance methods, and a pointer to the current class in class methods.

Another implied argument is _cmd, which is the selector that was sent to the method.

Please be aware that you only get self and _cmd in Obj-C methods. If you declare a C(++) method, for instance as a callback from some C library, you won’t get self or cmd.

For more information, see the Using Hidden Arguments section of the Objective-C Runtime Programming guide.

Yes, it’s exactly the same as “this” in Java – it points to the “current” object.

self is an object pointer to the current instances dispatch table. It is an implicit first argument to every member function of an object, and is assigned when that function is called.

In functions like init, you need to be careful that when you call the super class init you reassign self to be the return value as the super class init may redefine what self points to.

super is similar to self except it points to the superclass dispatch table.

Two important notes:

  1. The class itself, e.g. UIView (I’m NOT talking about a UIView object) is itself an object, and there is a self associated with it. So for example, you can reference self in a class method like this:

    // This works
    +(void) showYourself { [self performSelector: @selector(makeTheMostOfYourself)]; }
    
    // Class method!
    +(void) makeTheMostOfYourself { }
    
  2. Note that the compiler does NOT raise any warnings or errors, even if the self you mean to reference is an object and not a class. It is VERY easy to cause crashes this way, for example:

    // This will crash!
    +(void) showYourself { [self performSelector: @selector(makeTheMostOfYourself)]; }
    
    // Object method!
    -(void) makeTheMostOfYourself { }
    
    
    
    // This will crash too!
    -(void) showYourself2 { [self performSelector: @selector(makeTheMostOfYourself2)]; }
    
    // Class method!
    +(void) makeTheMostOfYourself2 { }
    

    Sadly, this makes class methods a bit harder to use, which is unfortunate because they are a valuable tool for encapsulation through information hiding. Just be careful.