Question

I have a following code in my singleton class

static MySingleton *gManager;
+(void)initialize
{
   if(self == [MySingleton class])
   {
       gManager = [[MySingleton alloc] initWithServices:[[MyServices alloc] init]];
   }
}
+(MySingleton *)sharedInstance
{
   return (gManager);
}

Unfortunately, during the unit tests I see that gManager is an instance of type SenTestCaseRun. I cant seem to figure out why? So a call like [[MySingleton sharedInstance] myFunction];

results in an error that myFunction is an unknown selector although it exists in the MySingleton class.

It is of type SenTestCaseRun because I checked using NSStringFromClass function.

Any pointers? Already banged my head for 3-4 hours on this :(.

Was it helpful?

Solution

Put a breakpoint in +initialize to make sure this variable is set correctly. If that doesn't explain it, use a watchpoint on it to see who's modifying it.

OTHER TIPS

it may be better to just put the initialization code inside the shared instance method

+(MySingleton *)shared
{
    static MySingleton *sharedInstance = nil;
    if(sharedInstance == nil){
        sharedInstance = [[MySingleton alloc] init];
    }
    return sharedInstance;
}

also in your code you are comparing an object to a class which will never be true instead of comparing [self class] to [MySingleton class].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top