Objective-C provides facility of class level access. In the examples given above we have used '-' sign before method, '-' means instance level access. Now we will see how to define method that can be access on class level.
MyClass.h
#import<Foundation/NSObject.h>
@interface MyClass:NSObject {
} -(void)instanceShow; +(void)classShow; @end |
MyClass.m
#import<stdio.h>
#import"MyClass.h"
@implementation MyClass
-(void)instanceShow {
printf("This is instance
level method.\n");
}
+(void)classShow {
printf("This is class
level method.");
}
@end
|
MyClassMain.m
#import<stdio.h>
#import"MyClass.m"
int main(){
MyClass *instance = [
[MyClass alloc]init];
[instance instanceShow];
[MyClass classShow]; [instance release]; return ; } |
Output: This is instance level method. This is class level method. |
Here in this example we have created a method named 'classShow'
that can be accessed on class level means no need to create object to use
classShow() method. We can directly use this method through class name. +(void)init method is called when objective-C program starts and it calls for
every class so it is the better place to define class level variable.
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: Class level access
Post your Comment