Reputation: 1
Here is my question:
In the viewDidLoad
method I create a variable using NSUserDefaults
(if its the "first run" I create it and fill it with NSNumber
. Then I try to use it in another method and ... nothing. It looks like its empty. Anyone can help me? Lot of Thanks
- (void)viewDidLoad {
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"seriesBool"]!=nil)
{
seriesBool = [defaults objectForKey:@"seriesBool"];
}
else
{
int i;
seriesBool = [NSMutableArray arrayWithCapacity:9];
for(i=0; i<9; i++)
{
[seriesBool addObject:[NSNumber numberWithBool:YES]];
}
}
-(IBAction)toAction:(id)sender
{
NSLog(@"array: %@", seriesBool);
}
seriesBool is empty...
Upvotes: 0
Views: 166
Reputation: 1751
You have to set properties like
@property(nonatomic,retain) NSMutableArray * seriesBool;
and use it
self.seriesBool = [NSMutableArray arrayWithCapacity:9];
or
use
seriesBool = [[NSMutableArray alloc] initWithCapacity:9];
instead of
seriesBool = [NSMutableArray arrayWithCapacity:9];
& u have to alloc and assign the object
seriesBool = [[NSMutableArray alloc] init];
seriesBool = [defaults objectForKey:@"seriesBool"];
Upvotes: 3
Reputation: 3874
You need to learn first the basics of Memory Management. What's happening there is, basically, that you're not retaining the seriesBool iVar.
Take a look here: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html
Try to retain your iVar or, better, create a strong/retain property and use the accessor methods.
Upvotes: 1