1

I want to create a class in objective-c with its methods, so that for accessing the data I don't want to instantiate the class. how can I do it?

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247

2 Answers2

3

Either you can use singleton, or if you are planning to use only static methods, you can just add it in the class and use it directly with class name.

Create methods as static,

+(void)method;

then use it as,

[MyClass method];

This is helpful only if you are creating some utility classes which has only some utility method like processing an image or so. If you need to have property variables, you will need singleton.

For eg:-

Go to new file and create MySingleton class which will create MySingleton.h and MySingleton.m files.

In .h file,

@interface MySingleton : NSObject 
{
 UIViewController *myview;
}

@property (nonatomic, retain) UIViewController *myview;

+(MySingleton *)sharedSingleton;

In .m file,

+ (MySingleton*)sharedSingleton {
    static MySingleton* _one = nil;

    @synchronized( self ) {
        if( _one == nil ) {
            _one = [[ MySingleton alloc ] init ];
        }
    }

    return _one;
}

- (UIViewController *)myview {
  if (!myview) {
    self.myview = [[[UIViewController alloc] init] autorelease]; //you can skip this, but in that case you need to allocate and initialize the first time you are using this. 
  }
  return myview;
}

Then use it as,

[[MySingleton sharedSingleton] myview] anywhere in your project. Remember to import MySingleton.h though. Similarly you can create any object in singleton and use it. Just implement the getter or setter method accordingly.

One thing you have to be careful is that the object created in a singleton has only a single memory space allocated and hence it is the same object whenever you are using anywhere in your project. The above code will not create multiple copies of myview object in the class. So whenever you are modifying a property of myview that will be reflected everywhere. Use this approach only if it is absolutely needed and you need to have access to a single object from all over the project. Normally we use this only for situations like storing a sessionID which needs to be accessed from different classes etc..

Community
  • 1
  • 1
iDev
  • 23,310
  • 7
  • 60
  • 85
  • how can I access the textview from this kind of implementation? – Giovanni Filaferro Oct 27 '12 at 10:14
  • 1
    I have updated my answer with answer to this question as well. Also why do you want to use textview like this instead of creating as local variable? any particular reason? – iDev Oct 27 '12 at 10:42
2

You may use singleton pattern, check this question.

Like this:

+(MySingleton *)sharedInstance {
    static dispatch_once_t pred;
    static MySingleton *shared = nil;
    dispatch_once(&pred, ^{
        shared = [[MySingleton alloc] init];
        shared.someIvar = @"blah";
    });
    return shared;
}

Or if you want to just access methods, you may use factory methods (those with +, not with -)

@interface MyClass
@property (nonatomic, assign) NSInteger value;

+ (void) factoryMethod;
- (void) instanceMethod;

...
// then in code
[MyClass factoryMethod]; // ok
[[MyClass sharedInstance] instanceMethod]; // ok
[MyClass sharedInstance].value = 5; // ok

UPDATE:

You may add a property to appDelegate

// in your app delegate.h
@property (nonatomic, retain) UIViewController* view;
// in your app delegate.m
@synthesize view;

and get appDelegate from almost any place like:

myapp_AppDelegate* appDelegate = [[UIApplication sharedApplicaton] delegate];
appDelegate.view = ...; // set that property and use it anywhere like this

Note, that you'll need to #import your UIViewController subclass and your appDelegate.h to make autocomplete work and sometimes avoid warnings.

// someFile.m
#import "appDelegate.h"
#import "myViewController.h"
...
myapp_AppDelegate* appDelegate = [[UIApplication sharedApplicaton] delegate];
appDelegate.view.myLabel.text = @"label text";    
Community
  • 1
  • 1
MANIAK_dobrii
  • 6,014
  • 3
  • 28
  • 55
  • I have already my class written and allocated but I can use it only where I have allocated it... Is there a way to use it ad a global variable Just like when I write in .h – Giovanni Filaferro Oct 27 '12 at 10:08
  • Anyway you need to store pointer to it somehow. Maybe you could add a property to your AppDelegate or create a singleton class (like in my answer) and add that pointer in there. – MANIAK_dobrii Oct 27 '12 at 10:10