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?
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.
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.
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.
-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.
It is the same as self
, only a redundant call .
[self self] // ---> Same object of self
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With