About the keyword `self`

+ (void)Foo;
- (void)Foo;

In a method, a - (void)Fookeyword selfmeans an instance of a class. But in the method + (void)Foo, what does the keyword mean self? Does this mean Class?

+5
source share
2 answers

selfis one of two implicit parameters for each method. This is a pointer to an object, and initially any object received a message to call the executable method. When the method in question is an instance method, it selfwill be an instance of the class in which the method is defined, or one of its subclasses. In the case of a class method, it selfwill be an object of the class.

+7
source

Yes. The following works fine:

+(id)myObjectWithInt:(NSInteger)anInt {
    return [[[self alloc] initWithInt:anInt] autorelease];
}
+5
source

All Articles