iOS: update object of NSMutableArray in for loop - ios

I need to update / add a key -> value pair in a NSMutableArray.
I would like to add a key with a fix value if the key isn't set already.
I tried the following - but the app is crashing at addObject with a "mutating method sent to immutable object" error:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *bookmarks = [defaults mutableArrayValueForKey:#"bookmarks"];
for (id bookmark in bookmarks) {
if ([bookmark objectForKey:#"type"] == NULL){
[bookmark addObject:#"old" forKey:#"type"];
}
}
[[NSUserDefaults standardUserDefaults] synchronize];

Your outer array contains immutable dictionaries. One option would be the following:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *bookmarks = [defaults mutableArrayValueForKey:#"bookmarks"];
[booksmarks enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSDictionary *bookmark, NSUInteger index, BOOL *stop) {
if (!bookmark[#"type"]) {
NSMutableDictionary *mutable = [bookmark mutableCopy];
mutable[#"type"] = #"old";
[bookmarks replaceObjectAtIndex:index withObject:[mutable copy]];
}
}];
// Update NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:#"bookmarks"];
This should be nice an efficient since it can update multiple dictionaries concurrently.

Try this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//SAVING
NSMutableArray *bookmarksSaving = [NSMutableArray new];
[bookmarksSaving addObject:[[NSMutableDictionary alloc] initWithObjects:#[#"red"] forKeys:#[#"color"]]];
[bookmarksSaving addObject:[[NSMutableDictionary alloc] initWithObjects:#[#"yellow"] forKeys:#[#"color"]]];
[bookmarksSaving addObject:[[NSMutableDictionary alloc] initWithObjects:#[#"green",#"new"] forKeys:#[#"color",#"type"]]];
[defaults setObject:bookmarksSaving forKey:#"bookmarks"];
[[NSUserDefaults standardUserDefaults] synchronize];
//MODIFY
NSMutableArray *bookmarks = [defaults mutableArrayValueForKey:#"bookmarks"];
NSUInteger index = 0;
for (NSDictionary *bookmark in bookmarks) {
NSMutableDictionary *bookmarkMutable = [bookmark mutableCopy];
if ([bookmarkMutable objectForKey:#"type"] == nil)
{
[bookmarkMutable setValue:#"old" forKey:#"type"];
[bookmarks replaceObjectAtIndex:index withObject:bookmarkMutable];
}
index++;
}
[[NSUserDefaults standardUserDefaults] synchronize];
You have to save your bookmarks as NSMutableDictionary to be able to add/remove keys/value

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *bookmarks = [defaults objectForKey:#"bookmarks"];
NSMutableArray *newBookMarks = [bookmarks mutableCopy];
for (NSDictionary *bookmark in bookmarks) {
NSMutableDictionary *newBookmark = [bookmark mutableCopy];
if (![bookmark objectForKey:#"type"]){
[newBookmark setObject:#"old" forKey:#"type"];
}
[newBookMarks addObject:newBookmark];
}
[defaults setObject:newBookMarks forKey:#"bookmarks"];
[defaults synchronize];

Related

Attempt to set a non-property-list object [duplicate]

This question already has answers here:
Attempt to set a non-property-list object as an NSUserDefaults
(12 answers)
Closed 8 years ago.
NSMutableArray *mutableArray = [NSMutableArray array];
Prostate *prostate = [Prostate new];
prostate.prostateCheckTimeString = self.prostateCheckTime.text;
prostate.prostateCubeString =self.prostateCube.text;
prostate.prostateAntigemString =self.prostateAntigem.text;
prostate.peeSpeedMaximumString =self.peeSpeedMaximum.text;
prostate.peeRemenderString =self.peeRemender.text;
[mutableArray addObject:prostate];
NSArray *tempArray = [[NSArray alloc]initWithArray:self.mutableArray];
[Global saveArrayToUserDefault:prostateArray saveValue:tempArray];
NSArray *array = [Global getArrayFromUserDefault:prostateArray];
//Below is my global object
**+(void)saveArrayToUserDefault:(NSString *)userDefaultKey saveValue:(NSArray *)value**
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:value forKey:prostateArray];
[userDefaults synchronize];
}
**+(NSArray *)getArrayFromUserDefault:(NSString *)userDefaultKey**
{
NSArray *temp;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
temp = [userDefaults objectForKey:userDefaultKey];
return temp;
}
-----------------------------------Description ---------------------------------------
I had Been Search for a while , I know NSUserDefault can only store NSArray , I tried to convert NSMutableArray to NSArray , but still got error
-----------------------------------Error ---------------------------------------------
Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType') Attempt to set a non-property-list object (
""
Implement the <NSCoding> protocol in your entity class which you wanna save
-(void)writeArrayWithCustomObjToUserDefaults:(NSString *)keyName withArray:(NSMutableArray *)myArray {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];
[defaults setObject:data forKey:keyName];
[defaults synchronize]; }
-(NSArray *)readArrayWithCustomObjFromUserDefaults:(NSString*)keyName {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:keyName];
NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
[defaults synchronize];
return myArray; }
Prostate is your custom object and NSUserDefaults does not allow you to save it.
So you have to Archive it before saving to NSUserDefaults and Unarchive it when reading back from NSUserDefaults.
So your methods to save and read user defaults data should look like:
-(void)writeArrayWithCustomObjToUserDefaults:(NSString *)keyName withArray:(NSMutableArray *)myArray {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:myArray];
[defaults setObject:data forKey:keyName];
[defaults synchronize];
}
-(NSArray *)readArrayWithCustomObjFromUserDefaults:(NSString*)keyName {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [defaults objectForKey:keyName];
NSArray *myArray = [NSKeyedUnarchiver unarchiveObjectWithData:data];
return myArray;
}

how to retain my uitextfield values in viewController when i calling?

I have a main page having 3 textfield in my first view as i navigate to different view and return back to my first view (i.e after i enter all the values i moving to next page).I want my 3 textfield to retain its value when i go back to my main page here my sample code :
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *mobile = MobileNum.text;
[defaults setObject:mobile forKey:self.MobileNum.text];
NSString *amountText = amount.text;
[defaults setObject:amountText forKey:self.amount.text];
[defaults synchronize];
NSString *temp = [defaults objectForKey:self.MobileNum.text];
NSString *temp1 = [defaults objectForKey:self.amount.text];
NSLog(#"%# %#",temp,temp1);
at you viewWillDisappear method store the defaults like.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *mobile = MobileNum.text;
[defaults setObject:mobile forKey:#"mobileNumber"];
NSString *amountText = amount.text;
[defaults setObject:amountText forKey:#"amount"];
[defaults synchronize];
again at your viewWillAppear method retrieve the values like.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults valueForKey:#"mobileNumber"] length]>0){
MobileNum.text=[defaults valueForKey:#"mobileNumber"];
}
else {
MobileNum.text=#"";
}
if ([[defaults valueForKey:#"amount"] length]>0) {
amount.text=[defaults valueForKey:#"amount"];
}
else {
amount.text=#"";
}
it will work.

NSUserDefaults can't update a nsmutable array

I saved a nsmutableArray inside a NSUserDefaults.
In the following case, it seems that all the elements from the array are equal to 0, even though in this case position 1 and position 5 should have 1 instead of 0 as a value. I know that NSUserDefaults elements are immutable but ...I did add that mutableCopy when retrieving the value.
Where am I wrong?
//create array
NSMutableArray *objArray = [[NSMutableArray alloc] init];
for (int i=0; i< 100;i++) [objArray addObject:#"0"];
[objArray replaceObjectAtIndex:1 withObject:#"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
// update
elementPosition = 5;
NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
[objArray replaceObjectAtIndex:elementPosition withObject:#"1"];
//check the array
NSMutableArray *objArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:nameFile]];
BOOL displayContent = true;
for (int i=0; i<[objArray count];i++)
{
if ([[objArray objectAtIndex:i] isEqualToString:#"0"])
{
displayContent = false;
}
}
I think when you retrieve and your mutable array from UserDefault and updated but you didn't set that new updated object to UserDefault, its having the old object which was set before. you have to store your update array again to userdefault with same key which update your UserDefautlt.
elementPosition = 5;
NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
[objArray replaceObjectAtIndex:elementPosition withObject:#"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
You should call the synchronize method
[[NSUserDefaults standardUserDefaults] synchronize];
I think you should fix your code follow my bellow code:
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
[[NSUserDefaults standardUserDefaults] synchronize];
...
NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
[objArray replaceObjectAtIndex:elementPosition withObject:#"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:nameFile];
[[NSUserDefaults standardUserDefaults] synchronize];
Replace your code with this:
//create array
NSMutableArray *objArray = [[NSMutableArray alloc] init];
for (int i=0; i< 100;i++){
[objArray addObject:#"0"];
}
[objArray replaceObjectAtIndex:1 withObject:#"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
//First mistake. Missing this. Without this line the data is not saved in NSUserDefaults
[[NSUserDefaults standardUserDefaults] synchronize];
// update
elementPosition = 5;
NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
[objArray replaceObjectAtIndex:elementPosition withObject:#"1"];
//Second Mistake. You did not update the NSUserDefaults again.
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
[[NSUserDefaults standardUserDefaults] synchronize];
//check the array
NSMutableArray *objArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]];
BOOL displayContent = true;
for (int i=0; i<[objArray count];i++)
{
if ([[objArray objectAtIndex:i] isEqualToString:#"0"])
{
displayContent = false;
}
//Third mistake. Once the BOOL is made false in an if block you have to make it true in the else block, otherwise the value of the BOOL will remain false even if it does not enter the if block.
else{
displayContent = true;
}
NsLog(#"ArrayIndex=[%d];DisplayContent=[%d]",i,displayContent);
}
Happy coding.

display data based on uiswitch state

I am trying to display data based on if the user has selected a switch. the user will have he ability to select either one or two switches to either both on or one or other on. Data will then be displayed according to the state of the switch.
I have managed to get this to load however its only displaying data from the dare array in the plist file and it dosnt matter which state the switch is in it displays dare not truth or both.
- (IBAction)shownext:(id)sender {
if (!self.plistArray) {
NSString *path = [[NSBundle mainBundle] pathForResource:
#"data" ofType:#"plist"];
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:#"truthonoff"] isEqualToString:#"YES"] && [[defaults objectForKey:#"dareonoff"] isEqualToString:#"YES"] ) {
NSDictionary *plistDict1 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray * plistArray1 = plistDict1[#"truth"];
NSDictionary *plistDict2 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray2 = plistDict2[#"dare"];
self.plistArray = [[plistArray1 arrayByAddingObjectsFromArray:plistArray2] mutableCopy];
} else if ([[defaults objectForKey:#"truthonoff"] isEqualToString:#"YES"] ) {
NSDictionary *plistDict3 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *plistArray3 = plistDict3[#"truth"] ;
self.plistArray = [plistArray3 mutableCopy];
NSLog(#"%#", plistArray);
} else ([[defaults objectForKey:#"dareonoff"] isEqualToString:#"YES"] ); {
NSDictionary *plistDict4 = [[NSDictionary alloc] initWithContentsOfFile:path];
NSMutableArray *plistArray4 = plistDict4[#"dare"];
self.plistArray = [plistArray4 mutableCopy];
NSLog(#"%#", plistArray);
}
}
}
-(void)stateSwitched:(id)sender {
UISwitch *tswitch = (UISwitch *)sender;
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
[defaults setObject: tswitch.isOn ? #"YES" : #"NO" forKey:#"truthonoff"];
[defaults synchronize];
}
-(void)stateSwitcheddare:(id)sender {
UISwitch *tswitch = (UISwitch *)sender;
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
[defaults setObject: tswitch.isOn ? #"YES" : #"NO" forKey:#"dareonoff"];
[defaults synchronize];
}
THis is my code for setting the object
Try to use boolForKey instead of objectForKey. And in the other hand, why does the else have a condition?:
if ([defaults boolForKey:#"truthonoff"] && [defaults boolForKey:#"dareonoff"]) {
// Both true
} else if ([defaults boolForKey:#"truthonoff"] && ![defaults boolForKey:#"dareonoff"]) {
// truthonoff true, dareonoff false
} else if (![defaults boolForKey:#"truthonoff"] && [defaults boolForKey:#"dareonoff"]) {
// truthonoff false, dareonoff true
} else {
// Both false
}
Make sure to do two things when you store boolean values in the NSUserStandardDefaults:
Use setBool setter: [[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"a_key"];
Remember to synchronize the standardUserDefaults to save the values.

mutating method sent to immutable object

When I use this method first time it works fine, but when I called it second time I get the error "mutating method sent to immutable object". The problem is at line with "addObject" command.
-(IBAction) save: (id) sender{
NSMutableArray *placesT= [[NSUserDefaults standardUserDefaults] objectForKey:#"placesT"];
if (!placesT) {
placesT=[[[NSMutableArray alloc] init] autorelease];
}
[placesT addObject: [NSString stringWithFormat:#"%#", tagF.text] ];
NSUserDefaults *tUD=[NSUserDefaults standardUserDefaults];
[tUD setObject:placesT forKey:#"placesT"];
[tUD synchronize];
[self dismissModalViewControllerAnimated:YES];
}
As the documentation for NSUserDefaults says: "Values returned from NSUserDefaults are immutable, even if you set a mutable object as the value." Whenever you want to change a collection you get from NSUserDefaults you have to get the immutable version, make a mutableCopy, modify that, and set it back again.
That is because the object stored in the NSUserDefaults is not the mutableArray but a normal array.
- (IBAction)save:(id)sender {
NSMutableArray *placesT = nil;
NSArray *tempArray = [[NSUserDefaults standardUserDefaults] objectForKey:#"placesT"];
if (tempArray) {
placesT = [tempArray mutableCopy];
} else {
placesT = [[NSMutableArray alloc] init];
}
[placesT addObject:[NSString stringWithFormat:#"%#", tagF.text]];
NSUserDefaults *tUD = [NSUserDefaults standardUserDefaults];
[tUD setObject:placesT forKey:#"placesT"];
[tUD synchronize];
[self dismissModalViewControllerAnimated:YES];
[placesT release];
}
placesT is a non mutable array, either always set placesT a mutable object always or use following code.
NSMutableArray *placesT= [[[NSUserDefaults standardUserDefaults] objectForKey:#"placesT"] mutableCopy];
This should work:
-(IBAction) save: (id) sender {
NSMutableArray *placesT= [[NSMutableArray alloc]initWithArray:[[NSUserDefaults standardUserDefaults]
objectForKey:#"placesT"]];
if (!placesT) {
placesT=[[[NSMutableArray alloc] init] autorelease];
}
[placesT addObject: [NSString stringWithFormat:#"%#", tagF.text] ];
NSUserDefaults *tUD=[NSUserDefaults standardUserDefaults];
[tUD setObject:placesT forKey:#"placesT"];
[tUD synchronize];
[self dismissModalViewControllerAnimated:YES]; }

Resources