1

I have a method that is working but its not saving the data that I enter.

This is the code I use to enter data on a

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
    NSString *tempTextField = [alertView textFieldAtIndex:0].text;

        if (!numbers) {
            numbers = [[NSMutableArray alloc] init];

        }
        [[NSUserDefaults standardUserDefaults] setObject:tempTextField forKey:@"Save"];
        [numbers insertObject:tempTextField atIndex:0];
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
        [self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } }

It is using a UIAlertView with a plain text input. I try to use an NSUSerDefaults to save the data with the method above and I'm able to retrieve the data on the viewDidLoad with this code

-(void)viewDidLoad {
[super viewDidLoad];
NSArray *siteNameValue = [[NSUserDefaults standardUserDefaults] valueForKey:@"Save"];

    numbers = [[NSMutableArray alloc] initWithObjects:siteNameValue, nil];
}

But it would only save one of the data that is entered, it doesnt save multiple data. Any leads?

the variable numbers is an NSMutableArray.

2
  • Do not use NSUserDefaults to save your app data. Commented Nov 27, 2015 at 21:13
  • @maddy I know it may not be efficient but I just need to use this for little data, not big stuff and I thought this would be an easy way of doing that. Commented Nov 27, 2015 at 21:26

3 Answers 3

1

You can store a NSMutableArray object created alertView:clickedButtonAtIndex in the user defaults and reuse it in viewDidLoad:

[numbers insertObject:tempTextField atIndex:0];
[[NSUserDefaults standardUserDefaults] setObject:numbers forKey:@"Save"];

You can get the array with same content in viewDidLoad as follows:

NSMutableArray *numbers = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"Save"] mutableCopy];
Sign up to request clarification or add additional context in comments.

5 Comments

You can't read mutable data from NSUserDefaults. You need to call mutableCopy on the NSArray you get from NSUserDefaults.
Thanks for pointing that out. I am a little bit rusty about objc. In Swift, you don't have to worry about this conversions as mutability is handled by var and let.
@ozgur would it make a difference if the variable numbers is already an NSMutableArray?
@CyrilIvarGarcia I don't think there is. You can safely store NSMutableArray or NSArray but you need to call mutableCopy when retrieving it as you'll get an immutable array from user defaults.
@ozgur awesome man, it worked! Thank you, I'll put it on top for other peoples reference.
0

Problem is you are saving just one NSString, not a NSArray in NSUserDefaults.

You need to save "numbers" (whole NSArray) instead and also load it as an array (additional encapsulation on loading is not necessary).

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
    NSString *tempTextField = [alertView textFieldAtIndex:0].text;

    if (!numbers) {
        numbers = [[NSMutableArray alloc] init];

    }
    [[NSUserDefaults standardUserDefaults] setObject:numbers forKey:@"Save"];
    [numbers insertObject:tempTextField atIndex:0];
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
} 
}

-(void)viewDidLoad {
    [super viewDidLoad];
    numbers = [[NSUserDefaults standardUserDefaults] valueForKey:@"Save"];
}

1 Comment

Hi @grzegorzkrukowski I thought about that too and tried it but my app would just crash. the variable numbers by the way is an NSMutableArray if that makes any different to your suggestion.
0

Solution

This is the method to ADD and SAVE data on the tableview using a UIALertView plain text style.

 -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        if (buttonIndex == 1) {

            NSString *tempTextField = [alertView textFieldAtIndex:0].text;

            if (!numbers) {
                numbers = [[NSMutableArray alloc] init];

            }
            [numbers insertObject:tempTextField atIndex:0];
            [[NSUserDefaults standardUserDefaults] setObject:numbers forKey:@"3"];
            [[NSUserDefaults standardUserDefaults] synchronize];

            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.myTableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
    }

below is to retrieve it under the viewDidLoad

  NSMutableArray *siteNameValue = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"3"] mutableCopy];


    numbers = siteNameValue;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.