Update @ 2012-08-07: As mentioned by Siby, the approach in this article may cause memory leak. Please try to use singleton as shown in the following link.
The Objective-C Singleton
Like Java, i would like to add a Static Class such that i could call static variables and static methods in any where.
In Objective-C, you can create a static variable using a static modifier.
For static method which is also known as class method, you can use the + sign instead of the – sign when declaring the method.
I have created a Utility class as follow.
Utility.h
@interface Utility : NSObject + (int)getNumber; + (void)setNumber:(int)number; @end
Utility.m
#import "Utility.h" @implementation Utility static int number = 1; + (int)getNumber { return number; } + (void)setNumber:(int)newNumber { number = newNumber; } + (id)alloc { [NSException raise:@"Cannot be instantiated!" format:@"Static class 'ClassName' cannot be instantiated!"]; return nil; } @end
Now i would like to get and set this static variable when loading HelloView. so I need to modify the HelloViewController.m.
HelloController.m
#import "Utility.h" @implementation HelloController ... - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"number = %d", [Utility getNumber]); [Utility setNumber:3]; NSLog(@"number = %d", [Utility getNumber]); } ... @end
Run the application and check the Debugger Console.
2010-04-09 17:27:18.864 HelloWorld[10860:20b] number = 1 2010-04-09 17:27:18.874 HelloWorld[10860:20b] number = 3
Done =)
Update @ 2011-08-25: Add (id)alloc in Utility.m to avoid the class being instantiated. Thanks Liam. =)
Hi Ying Kit,
Thanks for your post, this is what I’m looking for.
Keep up the good work! ^_^
Tuyen Nguyen
LikeLike
Thanks =)
LikeLike
Exactly what I was after – thanks!
LikeLike
you are welcome =)
LikeLike
You could also override ‘alloc’ to ensure the class is not instantiated:
// Class strictly cannot be instantiated
+(id)alloc {
[NSException raise:@”Cannot be instantiated!” format:@”Static class ‘ClassName’ cannot be instantiated!”];
return nil;
}
-Liam
LikeLike
I didn’t pay attention to that. Thanks for your suggestion. =)
LikeLike
Thanks so much!
LikeLike
You are welcome. Good to know that it could help. =)
LikeLike
thanks buddy
LikeLike
you are welcome~
LikeLike
How do I create a static object (like NSArray) in objective c?
LikeLike
The following link should help.
Objective-C Beginner’s Guide – Class level access
LikeLike
Hi Friends,
I have made a class Utilty like you define above, which has more than 10 static methods. i call Utility class methods using like [Utility getNumber].. But i want to ask as i have defined more than 10 methods in same class, how they load in memory, do they live all time in memory? or load at call time and release memory after it’s function has done.
I am very much confused about this issue, Please help me……………….
LikeLike
I have not much idea on this, but seems it will consume memory just like instance as mentioned in the following post.
StackOverflow – Object C static variable memory question
If you are really concern about the memory issue, you can use Instrument to monitor the memory usage.
Or instead of using a static class, you can create a singleton in objective c so in any particular of time, only one instance exists.
Cocoa Fundamentals Guide: Cocoa Objects
hope this help =)
LikeLike
Good write up, this got me going.
LikeLike
Thanks for your comment. =)
LikeLike
I wish all tutorials were this simple. Thanks! Using it for a Cocos2d game.
LikeLike
Good to know that i could help. =D
LikeLike
Why will anyone do this. This is as good as creating a global variable. Why don’t you create a singleton and implement the same. In this case since it is a Integer it may not leak memory. But if some one uses an NSString the re will be a memory leak. Please stick to the base patterns that is already used by so many people.
LikeLike
Thanks for your comment. I am really not good in dealing with memory management, When i wrote this post i just stick with what i know in Java.
Thanks for pointing out the problem.
LikeLike
I am still in search of such post about Abstract class in Objective C. Thank you very much for this post.
LikeLike
The solution in this post may not conform to the objective-c best practice. If you find any better solution, do let us know. =)
LikeLike