Reputation: 896
I want to add the another object to existing key in NSUserDefault
.
I try this code to add in NSUserDefault
.
delegate.str=[first objectAtIndex:delegate.selectId];
NSLog(@"%@",delegate.str);
delegate.str1=[details1 objectAtIndex:delegate.selectId];
NSLog(@"%@",delegate.str1);
[delegate.BookMarkDefault setObject:delegate.str forKey:@"Name"];
// BookMarkDefault This is NSUSERDefault.
First I will insert delegate.str
value value successfully goes to NSUSerDefault
, but when next time adding new value through delegate.str
for key under Name
will replace previous value. I want to add new value under the same Key.
Upvotes: 0
Views: 1981
Reputation: 9913
No its not possible to assign multiple values to same key. If you want to use multiple values to same key then use array or dictionary and assign them to the key.
Ex :
[[NSUserDefaults standardUserDefaults] setObject:YOUR_ARRAY forKey:@"Name"];
Hope it helps you.
Upvotes: 1
Reputation: 5081
This is not possible if you want to fullfill this kind of requirement then try to use array and then put that array into NSUSerDefault...
Try this code
delegate.str=[first objectAtIndex:delegate.selectId];
NSLog(@"%@",delegate.str);
delegate.str1=[details1 objectAtIndex:delegate.selectId];
NSLog(@"%@",delegate.str1);
[yourarray addObject:delegate.str];
NSLog(@"%@",delegate.Bookmarknamearray);
[delegate.BookMarkDefault setObject:youarray forKey:@"Name"];
Upvotes: 2
Reputation:
You can not assign two value in single key of NSUserDefaults, you must need to take another "KEY"
for each value . your can replace pervious value by using following code :
Just try with following code :
[[NSUserDefaults standardUserDefaults] setObject: delegate.str forKey:@"Name"];
EDITE:
But you can add array in NSUserDefaults, suchlike
self.golbalArr = [[NSMutableArray alloc]initWithObject:@"1", @"2",....,nil];
[[NSUserDefaults standardUserDefaults] setObject: self.golbalArr forKey:@"Name"];
Upvotes: 0
Reputation: 1532
It may be helpful
[[NSUserDefaults standardUserDefaults] setObject: delegate.str forKey:@"Name"];
Upvotes: 0
Reputation: 107231
You can't save two objects for same key.
In NSUserDefaults
and NSDictionary
keys will be unique (There can be only one object for a particular key).
If you need to do this, you can store the values in NSMutableArray
or NSDictionary
and add that to the NSUserDefaults like:
[[NSUserDefaults standardUserDefaults] setObject:dataArray forKey:@"Name"];
Upvotes: 0