Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my object out of scope in viewWillDisappear if i change the object's value?
    primarykey
    data
    text
    <p>So I have this class:</p> <pre><code>@interface AmountViewController : UITableViewController &lt;UITableViewDelegate, UITextFieldDelegate&gt;{ NSManagedObjectContext *managedObjectContext; NSManagedObject *selectedObject; NSDecimalNumber *amount; NSDecimalNumber *accountBalance; NSInteger textFieldRow; } - (void)textFieldDone:(id)sender; - (NSDecimalNumber *)absoluteValue:(NSDecimalNumber *)number; @property (nonatomic, retain) NSManagedObjectContext *managedObjectContext; @property (nonatomic, retain) NSManagedObject *selectedObject; @property (nonatomic, retain) NSDecimalNumber *amount; @property (nonatomic, retain) NSDecimalNumber *accountBalance; @property NSInteger textFieldRow; @end </code></pre> <p>Now in my implementation</p> <pre><code>- (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] &amp;&amp; ![managedObjectContext save:&amp;error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } } NSLog(@"Select Object Amount Value (after save): %@", [selectedObject valueForKey:@"amount"]); } [super viewWillDisappear:animated]; } </code></pre> <p>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. :/ </p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload