2

I am somewhat new to iOS 5 singletons and am using a singleton as documented here:

iOS 5 Singletons

Something like this:

MyManager.h

#import <Foundation/Foundation.h>
@interface MyManager : NSObject

//Data for section 1
@property(nonatomic,copy) NSString * section1a;
@property(nonatomic, assign) NSUInteger section1b;


//Data for section 2
@property(nonatomic,copy) NSString * section2a;
@property(nonatomic, assign) NSUInteger section2b;

+ (id)sharedInstance;
@end

MyManager.m

@implementation MyManager
@synthesize section1a, section1b, section2a; , section2b;

+ (id)sharedInstance
{
    static dispatch_once_t pred = 0;
    __strong static id _sharedObject = nil;
    dispatch_once(&pred, ^{
        _sharedObject = [[self alloc] init]; // or some other init method
    });
    return _sharedObject;
}
@end

So I use it as follows:

MyManager * myManager = [MyManager sharedInstance];
myManager.data = self.data

Is this how you generally use the singleton? Am I missing anything? Sorry for the basic questions I just want to make sure I am doing it right.

Thanks

Strong Like Bull
  • 11,155
  • 36
  • 98
  • 169
  • I'm not sure which differences do you mean in iOS 5 or other version. But I think you missing declare this in .m file: static MyManager *_sharedObject = nil; – Matrosov Oleksandr Jul 19 '12 at 15:45
  • just for your sake, you would implement these two methods in your singleton class (besides the `+sharedInstance` method) `+allocWithZone:` and `+copyWithZone:`, and you can add a `static MyManager *_sharedObject;` member for your class as well. :) – holex Jul 19 '12 at 15:51

4 Answers4

1

This is not a singleton because you can create multiple instances of this class with alloc/init methods. A right answer is here

Community
  • 1
  • 1
iSofTom
  • 1,718
  • 11
  • 15
1

You're doing it right. This is (currently :) the way to go and works fine with ARC enabled, multiple threads etc.

It isn't strictly a singleton, as you can alloc more instances of the class, but I don't think that's relevant in your case.

Andreas Ley
  • 9,109
  • 1
  • 47
  • 57
  • can you help edit my code a bit so that it acts like a true singleton? – Strong Like Bull Jul 19 '12 at 16:34
  • I would actually advise against it, as there is almost never a reason to do so. The code you've posted works fine as it is. A "real" singleton would mean fiddling around with manual memory management - something that I'm glad I rarely have to do these days. :) – Andreas Ley Jul 19 '12 at 16:40
0

I usually make my singleton as such (suppose we are making a singleton for Manager):

static (Manager *) instance;

+ (Manager *) getInstance
{
    if(!instance) instance = [[Manager alloc] init];
    return instance;
}

This is a very basic singleton (it doesn't take into account multithreading or other advanced features) but this is the basic format.

Nosrettap
  • 10,940
  • 23
  • 85
  • 140
  • 1
    Bad idea, multithreaded code will make this fail faster than pulling the plug on your desktop. – Richard J. Ross III Jul 19 '12 at 15:47
  • I agree that multithreading code will make this fail; however, he never states that he wants a thread-safe singleton. I was just showing him the bare bones version – Nosrettap Jul 19 '12 at 15:49
  • @Nosrettap the whole point of a singleton is one instance. If multiple instances can be allocated, then this is not a singleton! – Richard J. Ross III Jul 19 '12 at 15:50
  • 1
    @RichardJ.RossIII If the program does not access this with multiple threads, then there will only be one instance. – Nosrettap Jul 19 '12 at 15:56
0

Refer to this: Singleton in iOS 5? You should override allowWithZone to prevent rogue code from creating a new instance.

Community
  • 1
  • 1
Καrτhικ
  • 3,833
  • 2
  • 29
  • 42