Objective-C methods are compiled into C functions, which are stored in the program's Text segment along with any other C functions the program defines. That's true for both class and instance methods.
where static / class methods are stored in objective c
-
19-09-2022 - |
Question
Suppose we declare a class as follows in MyClass.h
file as follows.
#import <Foundation/Foundation.h>
@interface MyClass : NSObject
+(void)aClassMethod;
@end
And in MyClass.m
File..
@implementation MyClass
+(void)aClassMethod{
NSLog(@"It is a Class method");
}
@end
My question is that after compilation where this aClassMethod
will be stored??
and if we declare some member functions , then where they will be stored.
Solution
OTHER TIPS
Actually I wanted to know that where class method is stored in stack , code or data segment?? And what tasks are done bye the compiler to handle the class methods?
This question doesn't fully make sense.
The executable code is stored where all executable code is stored; in the mach-o TEXT segments. The code is mapped into memory on readonly, executable, pages of memory. The memory cannot be made readwrite (i.e. you can't edit the executable in memory).
Objective-C methods are standard C functions that always take two arguments; self
and _cmd
.
In terms of calling convention, the code is called like any other C function. The only difference being the method dispatch mechanism that matches the call site ([NSObject new]
) to the executable code to be invoked. That matching is done through the Objective-C messenger and is implemented by objc_msgSend()
or some variant.