-1

I have a JSONModel class in my objective-c application. I use this class with a singleton then initialize this class with this code:

MyClass *client = [[MyClass alloc] init];
client = [[MyClass alloc] initWithDictionary:myDictionary error:nil];

.h

@interface MyClass : JSONModel
...
+ (id)sharedInstance;
...
@end

.m

static MyClass *singletonObject = nil;

+ (id) sharedInstance
{
    if (! singletonObject) {

        singletonObject = [[MyClass alloc] init];
    }
    return singletonObject;
}

- (id)init
{
    if (! singletonObject) {
        singletonObject = [super init];
    }
    return singletonObject;
}

I'm trying check if I had initialize my class like:

if([MyClass sharedInstance] == nil){

But it isn't working... How can I check if was initialized?

user3745888
  • 6,143
  • 15
  • 48
  • 97
  • 1
    Of course that doesn't work -- `sharedInstance` initializes the object if it was `nil` before returning it. This feels like an [X/Y problem](https://meta.stackoverflow.com/questions/254341/a-car-with-square-wheels); what are you trying to do? – NobodyNada Mar 09 '17 at 21:27
  • I Initialize MyClass with data received from server in a call but then, I want delete this and recall so I'm checking when is empty to do the call and when no,, Hope be clear – user3745888 Mar 09 '17 at 21:30
  • 2
    I'm really not sure what you're doing here. Why do you even need a singleton at all? Also, what's going on in your `init` method? I think you may even be violating memory management conventions, which will result in really weird bugs eventually. – NobodyNada Mar 09 '17 at 21:33
  • There's millions of examples of how to implement singletons in objective-c. Why not just copy one instead of inventing your own, which you have discovered doesn't work. And no, your comment is not clear at all. – Gruntcakes Mar 09 '17 at 22:34
  • I copied my singleton. My singleton works correctly, because I can load MyClass from another class with [MyClass sharedInstance] and it's work, but I want destroy this in a clas for check if is empty in other class and recall to server again.. Thanks for the answers – user3745888 Mar 10 '17 at 05:49

4 Answers4

1

Normally to init a singleton, you use a class method (in this case sharedInstance), and in that class method, you init your class if it is nil. Then calling in anywhere else like this to access it.

MyClass *yourMom = [MyClass sharedInstance];

// you can use yourMom from now

and it will remain in memory as long as app is not terminated.

GeneCode
  • 7,545
  • 8
  • 50
  • 85
0

I think we need to get back to basics here. Returning a singleton from an init method is not really a good idea, that's what your sharedInstance class method is for. In general methods should always do exactly what they say and no more hence an init method is there to initialize (init) a new instance and not just return a singleton. Also the static variable should really be declared inside the class method. This is generally how these types of methods are used but as others have stated we really need to know what type of behaviour you're after and why.

0

If you want to create a singleton, here is an example:

.h

@interface MyClass : JSONModel
...
@property(strong, nonatomic) Blah *blah; //NSMutableDictionary if you are trying to store JSON

+ (id)sharedInstance;
...
@end

.m

+ (id) sharedInstance {
    @synchronized(self) {
        if (! singletonObject) {
            singletonObject = [[MyClass alloc] init];
        }
        return singletonObject;
    }
}

//getter
- (Blah *)blah {
    if (!blah) {
        blah = [[Blah alloc] init];
    } 
    return blah;
}

And you wanna store your JSON into that singleton???

MyClass *client = [MyClass sharedInstance];
client.blah = ...//your JSON dictionary or something else. This can be used as a global variable.

I'm not sure what you are trying to do here, I don't event know why you want a singleton, do you want it as a global variable storing your JSON model to be used across the app?

Dovahkiin
  • 1,239
  • 11
  • 23
  • I want a singleton because I want get my JSONModel from another class, with the content initializaed by json, but if my content change, and I return to my first class where I initialized my class this not reload, because it has the json added already.. – user3745888 Mar 10 '17 at 07:24
0

Every time you used alloc/init you was just creating another instance instead of using the share one. If you want to init the share instance in a different way than just init, try something like that:

MyClass *client = [MyClass reinitSharedInstanceWithDictionary: myDictionary];

.h

@interface MyClass : JSONModel
...
+ (id)sharedInstance;
+ (id)reinitSharedInstanceWithDictionary:(NSDictionary*)dict;
...
@end

.m

static MyClass *singletonObject = nil;

+ (id) sharedInstance
{
    if (! singletonObject) {

        singletonObject = [[MyClass alloc] init];
    }
    return singletonObject;
}

+ (id) reinitSharedInstanceWithDictionary:(NSDictionary*)dict
{
    singletonObject = [[MyClass alloc] initWithDictionary:dict error:nil];
    return singletonObject;
}

- (id)init
{
    if (! singletonObject) {
        singletonObject = [super init];
    }
    return singletonObject;
}
VitorMM
  • 1,060
  • 8
  • 33