1

当我保存数组时,一切都很好

NSMutableArray *savestring = news;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:@"savestring"];
[defaults synchronize];

NSLog是一个标题,大约 10 个标题(从输出中很简单):

2013-08-19 11:23:13.556 Dr.Saleh[4282:1303] saveString= (
    "Sumou Real Estate Company Won The Award of the World Confederation of Business"
)
2013-08-19 11:23:13.558 Dr.Saleh[4282:1303] saveString= (
    "Biggest  indoor Mapping Projection event"
)
2013-08-19 11:23:13.559 Dr.Saleh[4282:1303] saveString= (
    "Sumou Real Estate launches"
)

.......ETC

然后当我加载字符串时,它只取最后一个字符串,这是加载代码:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *loadstring = [defaults objectForKey:@"savestring"];
[defaults setValue:loadstring forKey:@"savestring"];
news = [defaults mutableArrayValueForKey:@"savestring"];

NSLog新闻只给了我最后一个字符串保存在保存字符串中我的错误在哪里

//================================================= ============================//

我通过更改此代码解决了我的问题

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:@"savestring"];
[defaults synchronize];

让它像那样

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[[NSUserDefaults standardUserDefaults] setObject:[[[NSUserDefaults   standardUserDefaults] objectForKey:@"savestring"] arrayByAddingObject:news]     forKey:@"savestring"];
4

1 回答 1

1

假设您的“新闻”是数组类型,标题对象就是您所指的!!!,

NSMutableArray *savestring =[[NSMutableArray alloc]initWithArray:news];
//news is an array which has the titles..
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:@"savestring"];

或者

您可以直接保存它而无需将其分配给 savestring...

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:news forKey:@"savestring"];
[defaults synchronize];
于 2013-08-19T08:45:10.447 回答