Objective-C – Create a Static Class

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. =)

Advertisements

22 thoughts on “Objective-C – Create a Static Class”

  1. 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

    Like

  2. 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……………….

    Like

    1. 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 =)

      Like

  3. 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.

    Like

    1. 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.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s