[Solved] All NSUserDefaults, all in tableview


You can set an array to userDefaults, the implementation is very simple.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self addTextToUserDefaults:@"hello"];
    [self addTextToUserDefaults:@"how are you?"];
    [self addTextToUserDefaults:@"hi"];

    for (NSString *text in [self textsInUserDefaults]) {
        NSLog(@"%@", text);
    }
}

- (void)addTextToUserDefaults:(NSString *)aText
{
    NSMutableArray *texts = [[[NSUserDefaults standardUserDefaults] objectForKey:@"textArray"] mutableCopy];
    if (!texts) {
        texts = [NSMutableArray new];
    }
    [texts addObject:aText];

    [[NSUserDefaults standardUserDefaults] setObject:texts forKey:@"textArray"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSArray *)textsInUserDefaults
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:@"textArray"];
}

So you can simply copy addTextToUserDefaults and textsInUserDefaults methods
Call [self addTextToUserDefaults:textField.text]; to add text,
Call [self textsInUserDefaults] to retrieve the array of texts.

0

solved All NSUserDefaults, all in tableview