-1

Why can self be used in implementation methods, or in class methods? What's the internal structure of self? eg:

#import "Person.h"

@implementation Person
+ (void)initialize
{
    [super initialize];
    [self setupModel];
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self test];
    }
    return self;
}
+ (void)setupModel
{
    NSLog(@"setup a person object");
}
- (void)test
{
    NSLog(@"this method is called");
}
@end

Code run results: 2018-09-17 09:32:40.615578+0800 Test[662:301213] setup a person object 2018-09-17 09:32:40.615662+0800 Test[662:301213] this method is called

Code run results

li.cheng
  • 9
  • 1
  • 4
  • If `self` is used in a class method, doing `[ClassName otherClassMethod]` is the same as `[self otherClassMethod]` But to clarify, you might want to show also what are your code/called and the logs produced (in copy/pasted, not screenshot) – Larme Sep 14 '18 at 09:45
  • Please don't post text as a picture. Copy and paste the text of your code run results into your question as text. It's much easier to read, reference, search, and post. – rmaddy Sep 14 '18 at 16:45

2 Answers2

2

For instance methods, self is a reference to the current object.
For class methods, self is a reference to the current class.

If you declare an Objective-C method like this

- (void)doSomethingWithObject:(id)object { ... }

the compiler basically just turns it into a a C function with the following signature:

void _i_classname_doSomethingWithObject_(const id self, const SEL _cmd, id object) { ... }

(where classname is the name of the class the method belongs to. for class methods, the i becomes a c)


When you call the method [foo doSomethingWithObject:bar], the compiler turns that method call into a call to objc_msgSend, which "forwards" the call to the correct implementation:

objc_msgSend(foo, @selector(doSomethingWithObject:), bar);

As you can see, the parameters passed to objc_megSend are the same expected by the C function containing the implementation of the Objective-C method.

lukas
  • 2,300
  • 6
  • 28
  • 41
  • What's the bottom layer of self, is it a structure? @Lukas – li.cheng Sep 17 '18 at 01:59
  • @li.cheng pretty much, yeah. `self` has the same "structure" as any other instance of an objc class. basically, all classes are C structs w/ a so called `isa` pointer pointing to the object's class. (see this guide for more information: https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html ) – lukas Sep 22 '18 at 10:27
1

These are implementation details, so you shouldn't rely on them:

self is either a tagged pointer or a genuine pointer to an struct that starts with the isa field and then contains all of the object's instance variables.

The isa field is itself either a set of bitfields that contains the instance's retain contain, some flags, and a pointer to the Class of the instance, or (on older and 32-bit platforms) it is just a pointer to the Class of the instance (in which case the retain count is stored in a global hash table).

rob mayoff
  • 375,296
  • 67
  • 796
  • 848