Why is my object out of scope in viewWillDisappear if i change the object's value?
So I have this class:
@interface AmountViewController : UITableViewController <UITableViewDelegate, UITextFieldDelegate>{
NSManagedObjectContext *managedObjectContext;
NSManagedObject *selectedObject;
NSDecimalNumber *amount;
NSDecimalNumber *accountBalance;
NSInteger textFieldRow;
}
- (void)textFieldDone:(id)sender;
- (NSDecimalNumber *)absoluteValue:(NSDecimalNumber *)number;
开发者_StackOverflow@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSManagedObject *selectedObject;
@property (nonatomic, retain) NSDecimalNumber *amount;
@property (nonatomic, retain) NSDecimalNumber *accountBalance;
@property NSInteger textFieldRow;
@end
Now in my implementation
- (void)viewWillDisappear:(BOOL)animated {
NSLog(@"Select Object Amount Value: %@", [selectedObject valueForKey:@"amount"]);
if (amount != nil) //amount is out of scope, has something to do with retain, but accountBalance is in scope. ???
{
NSLog(@"Textfield Amount: %@", amount);
//TODO: save changes to amount
//[selectedObject setValue:amount forKey:@"amount"];
NSError *error = nil;
if (managedObjectContext != nil) {
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
NSLog(@"Select Object Amount Value (after save): %@", [selectedObject valueForKey:@"amount"]);
}
[super viewWillDisappear:animated];
}
For some reason, the amount object is 'out of scope' only if i modify its value before reaching the viewWillDisappear method. So for instance, I load this view and press back, everything is good. But if I change the amount value then press back, the debugger shows that the amount is out of scope. My thinking here is that the object is getting released prior to reaching the viewWillDisappear method, but I'm not sure what exactly to do. I tried other variations of (atomic/nonatomoic, retain/assign/copy) but I'm not sure I really understand all that even after reading about them. :/
Also, the selectedObject, accountBalance and other objects are still in scope even though they have the same property attributes. Any help is appreciated. Thanks. Let me know if I can provide more info for you.
You're right. It could be that amount
is getting released when you change its value. When you're setting the new value for amount
, its previous owner may be releasing it. But, if you are setting the new value directly to the instance variable, you are not retaining it.
To actually use the setters and getters that are synthesized according to your property rules (i.e. retain, etc.), you must access them as follows:
self.amount
self.amount = newValue;
You are bypassing the synthesized setters and getters if you access the instance variables directly, as follows:
amount
amount = newValue;
If you always use self.amount
rather than amount
to refer to your instance variable, you may save yourself a lot of trouble.
精彩评论