Reputation: 6448
I am reading some Objective-C code in a well-maintained GitHub repo. https://github.com/soffes/sskeychain/blob/master/SSKeychain.m
I came across some weird lines (at least weird to my eyes).
+ (NSArray *)allAccounts {
return [self accountsForService:nil error:nil];
}
I was taught that self refers to the instance itself in an instance method. So what does self mean here, in a class method?
Upvotes: 2
Views: 293
Reputation: 727047
Inside class methods, self
refers to the object representing the corresponding Class
:
+ (NSArray *)allAccounts {
NSLog("%@", [self description]); // Will print the name of the class
return [self accountsForService:nil error:nil];
}
This is the same object that you get in an instance method when you call [self class]
.
It is useful if you would like to call methods on the class
polymorphically. For example, you can call [[self alloc] init]
to create a new instance of the class on which the call is performed.
Upvotes: 1