NSNumber is out of scope?
I wrote an Objective-C class and I am using a generic instance of it across multiple views in my iPhone project. Its member variables include bools, ints, NSStrings, and one NSNumber. The shared instance seems to work fine within my application, with the exception of the NSNumber which the debugger tells me to be "out of scope" once the shared instance was available for a second or subsequent times.
Here's a quick overview of what I am 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 opens in one view like this:
UserData *userData userData = [[UserData getSharedUserData] retain];
And in another form in the same way. But the second time it is contacted, the gross item goes out of scope, but everything else is fine - that's why I'm stumped. Any ideas?
a source to share
Why did you write aksessury grossWage
( updateGrossWage
and getGrossWage
) by hand? And are you sure you just want to simply assign a given gross wage instead of storing or copying? This way, when the caller gets rid of their gross payroll account, you will receive the released gross payroll at the facility userData
:
NSNumber grossWage = [[NSNumber numberWithInt:12] retain];
[userData updateGrossWage:grossWage];
[grossWage release];
// Now userData’s grossWage points to released object.
This could be the cause of the problem. If not, Id suggests posting a smaller portion of the complete code example - with no notification material and with calling context.
PS Common objects like yours userData
are usually bad for your design (= lead to pain in the code), see, for example, this article from Miško He and other articles on his blog.
a source to share
The reason you are having problems is not because you should or should not use the property, but because you are not following memory management rules. NSNumber is an object and should be stored in your setter. Changing its property will fix the immediate problem as Objective-C handles your work, but you should still consider memory management because you are 100% sure that you will have problems until you are comfortable.
a source to share
Following to @zoul ...
I would like to declare that grossWage is a property and synthesizes getters and setters. I think the setter is the source of your problem.
// in UserData.h
@interface UserData : NSObject {
NSNumber *grossWage;
}
@property (nonatomic, retain) NSNumber *grossWage;
// in UserData.m
#import "UserData.h"
@implementation UserData
@synthesize grossWage;
// then do NOT create getters and setters for grossWage
See if it cleared up.
a source to share