Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does [self self] mean in objective c?

I was just reading some source code of https://github.com/MugunthKumar/MKNetworkKit, and saw this

    +(void) initialize {

  if(!_sharedNetworkQueue) {
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
      _sharedNetworkQueue = [[NSOperationQueue alloc] init];
      [_sharedNetworkQueue addObserver:[self self] forKeyPath:@"operationCount" options:0 context:NULL];
      [_sharedNetworkQueue setMaxConcurrentOperationCount:6];

    });
  }            
}

what does that [self self] mean here?

like image 498
CarmeloS Avatar asked Sep 21 '12 09:09

CarmeloS


People also ask

What is a self in Objective-C?

self is a special variable in Objective-C, inside an instance method this variable refers to the receiver(object) of the message that invoked the method, while in a class method self will indicate which class is calling.

Is self a keyword in C?

this, self, and Me are keywords used in some computer programming languages to refer to the object, class, or other entity of which the currently running code is a part.

Is there a self keyword in C++?

Within the implementation of a method, the identifier Self references the object in which the method is called. The Self variable is an implicit parameter for each object method. A method can use this variable to refer to its owning class.


2 Answers

-self is a method defined in the NSObject protocol. It returns the receiver, that is, the object you send the message self to. If you do [a self], you get a back, and yes, if you do [self self] (or self.self), you indeed get self back.

It may be useful in key-value paths where you are supposed to append a new component, but intend to observe the entire object, like in Cocoa Bindings. I don't see any application of this in the code you posted, but it may be the case that proxies adopt self differently, to point to the proxy itself, rather than the remote/forwarded object.

like image 97
Jesper Avatar answered Sep 28 '22 12:09

Jesper


It is the same as self, only a redundant call .

[self self] // ---> Same object of self
like image 31
aleroot Avatar answered Sep 28 '22 11:09

aleroot