Hans
Hans

Reputation: 79

How to retain the values of an array

I have two view controllers. The first one has a segue to another view controller. What I want for the latter view controller is to add items coming from a text field then save it in an array through a button click. Then this array will be saved using NSUserDefaults.

On the code below, I can store items in my array successfully. This array is also saved using NSUserDefaults. Yet, when I get back to my first view controller, and then proceed again to my 2nd view controller to add new items on my array, the items I stored earlier were gone. The newly added items were the only ones saved.

Advice please :)

in my .h file

@interface AddCardViewController : UIViewController <UITextFieldDelegate>

@property (strong, nonatomic) IBOutlet UITextField *cardNameTextField;

@end

in my .m file

@interface AddCardViewController ()

@property (nonatomic, strong)NSMutableArray *nameOfCards;

@end


@implementation AddCardViewController 

@synthesize cardNameTextField = _cardNameTextField;

@synthesize nameOfCards = _nameOfCards;


// setting my array
- (NSMutableArray *)nameOfCards 
{
    if (!_nameOfCards) _nameOfCards = [[NSMutableArray alloc] init];

    return _nameOfCards;
}

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    self.cardNameTextField.delegate = self;
}


- (BOOL) textFieldShouldReturn:(UITextField *)textField{

    [self.cardNameTextField resignFirstResponder];

    return YES;
}

- (void)viewDidLoad
{
    //getting the data saved

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSMutableArray *cardName = [defaults objectForKey:@"cardName"];

    NSLog(@"contents of my array %@", cardName);

    [super viewDidLoad];

}

- (IBAction)addNewCard:(id)sender {
    [self.nameOfCards addObject:self.cardNameTextField.text];
    NSLog(@"contents :%@", _nameOfCards);


    // this is how to save the data

    NSMutableArray *cardName = _nameOfCards;

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:cardName forKey:@"cardName"];

    [defaults synchronize];

    NSLog(@"data saved");

}


@end

Upvotes: 0

Views: 190

Answers (4)

note-knotz
note-knotz

Reputation: 173

it is better for you to use NSMutableArray than NSArray since you can edit, add and delete elements from NSMutableArray. Hope this helps.

Upvotes: 0

AMayes
AMayes

Reputation: 1767

Try using a singleton, instead. See my answer here: https://stackoverflow.com/a/10093449/542400 but make it an NSArray instead of a dictionary.

Upvotes: 0

The iOSDev
The iOSDev

Reputation: 5267

as the code given above shows that you just set the newly created cardname object i think you have to first fetch all data from nsuserdefaluts and add them to your _nameOfCards array and then add your new objects in addNewCard:(id)sender method like

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

NSMutableArray *cardName = [defaults objectForKey:@"cardName"];
self.nameOfCards = cardName;

[self.nameOfCards addObject:self.cardNameTextField.text];

NSMutableArray *finalCardNames = [[NSMutableArray alloc] initWithArray:self.nameOfCards copyItems:YES];

[defaults setObject:finalCardNames forKey:@"cardName"];

[defaults synchronize];

i think this will solve your problem try it have nice day

Upvotes: 0

Ken Thomases
Ken Thomases

Reputation: 90651

Objects retrieved from NSUserDefaults are always immutable, even if you set a mutable object for a key. You can think of it as NSUserDefaults making a copy.

If you need a mutable object based on what is stored in NSUserDefaults, make a mutable copy. For example:

NSMutableArray *cardName = [[[defaults objectForKey:@"cardName"] mutableCopy] autorelease];

Upvotes: 2

Related Questions