I've written an Objective-C class and I'm using a shared instance of it across several of the views in my iPhone project. Its member variables include bools, ints, NSStrings and one NSNumber. The shared instance seems to work just fine across the scope of my application, except for the NSNumber which the debugger tells me is "out of scope" once the shared instance has been accessed for the 2nd or subsequent times.
Here's a quick overview of what I'm doing...
// UserData.h
@interface UserData : NSObject {
TaxYears selectedTaxYear;
NSNumber *grossWage; // <--- this is the troublesome member
// ...
NSString *other;
int age;
}
+ (UserData *)getSharedUserData;
@end
// UserData.m
#import "UserData.h"
@implementation UserData
static UserData *sharedUserData = nil; // Points to the shared object
+ (UserData *)getSharedUserData {
if(sharedUserData == nil) {
sharedUserData = [[UserData alloc] initWithTaxYear:0];
[[NSNotificationCenter defaultCenter]
addObserver:sharedUserData
selector:@selector(doTerminate:)
name:UIApplicationWillTerminateNotification
object:nil];
}
return sharedUserData;
}
- (id)initWithTaxYear:(TaxYears)theTaxYear {
if ((self = [super init])) {
}
return self;
}
- (void)updateGrossWage:(NSNumber *)theGrossWage {
grossWage = theGrossWage;
}
- (NSNumber *)getGrossWage {
return grossWage;
}
// ...
@end
So it is accessed in one view like this:
UserData *userData
userData = [[UserData getSharedUserData] retain];
And in another view in the same way. But the second time it is accessed, the grossWage member is out of scope, but everything else is fine - this is why I'm stumped. Any ideas?