将新输入字符串保存到NSMutableArray iPhone [英] Saving new input strings to NSMutableArray iPhone

查看:78
本文介绍了将新输入字符串保存到NSMutableArray iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码添加字符串到 NSMutableArray 。但我面临的问题是,每当我添加一个新的值到 NSMutableArray ,以前的值被删除,新的值,所以总是有一个值。 / p>

我必须将此 NSMutableArray 保存到 NSUserDefaults 我会用它在其他屏幕上。



我已搜索alot,请帮助我



代码是:

   - (IBAction)addChildren:(id)sender {
inputChildName = nameOfChild.text;
childArray = [[NSMutableArray alloc] init];
[self.childArray addObject:inputChildName];
NSUserDefaults * childrenNamesForLabel = [NSUserDefaults standardUserDefaults];
[childrenNamesForLabel setObject:childArray forKey:@children];
[childrenNamesForLabel synchronize];
NSLog(@CHILDREN ARRAY :::%@,childArray);
}


解决方案

因为每当你收到addChildren您将重新创建childArray,这意味着它将包含0个对象,然后您添加一个对象。

  childArray = [[NSMutableArray alloc] init]; 
[self.childArray addObject:inputChildName];

移动 childArray = [[NSMutableArray alloc] init]; 到init或load方法,它应该工作。


I am using below code to add strings to NSMutableArray. But the issue I am facing is that whenever I add a new value to the NSMutableArray, previous value gets deleted and new value comes in, so there is always a single value.

I have to save this NSMutableArray to NSUserDefaults so that I would use it on other screens too.

I have searched alot please do help me

code is:

- (IBAction)addChildren:(id)sender {
    inputChildName = nameOfChild.text;
    childArray = [[NSMutableArray alloc] init];
    [self.childArray addObject:inputChildName];
    NSUserDefaults *childrenNamesForLabel = [NSUserDefaults standardUserDefaults];
    [childrenNamesForLabel setObject:childArray forKey:@"children"];
    [childrenNamesForLabel synchronize];
    NSLog(@"CHILDREN ARRAY::: %@", childArray);
}

解决方案

Because whenever you receive your addChildren action you re-create the childArray which means it will contain 0 objects, then you add one object.

childArray = [[NSMutableArray alloc] init];
[self.childArray addObject:inputChildName];

Move childArray = [[NSMutableArray alloc] init]; to an init or load method and it should work.

这篇关于将新输入字符串保存到NSMutableArray iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆