insert object in an NSMutableArray with NSmutableDictionary in UserDefaults - objective-c

I have an NSMutableArray. The array is storing NSMutableDictionary. The dictionary object is storing NSUserDefault values. These are slider values previously selected by user. Now I want that, when user move these sliders then new slider values must be saved in NSUserDefaults.
I'm using this method to add a value in the NSMutableArray
NSMutableArray *quarterValueArray = [[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"] mutableCopy];
if (!quarterValueArray)
{
NSMutableArray *aQuarterArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++)
{
NSMutableDictionary *dict1 = [[[NSMutableDictionary alloc] init] mutableCopy];
[dict1 setObject:#"" forKey:#"quarterP"];
[dict1 setObject:#"" forKey:#"quartersliderValue"];
[dict1 setObject:#"" forKey:#"totalOfTheQuater"];
[aQuarterArray addObject:dict1];
}
[[NSUserDefaults standardUserDefaults] setObject:aQuarterArray forKey:#"QuarterValues"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
and then for storing slider values i am using this method in sliderviewCntroller
-(void)saveUserDefaultDataQuarter
{
NSMutableDictionary *quarter = [QuarterValueArray objectAtIndex:0];
[quarter setObject:totalQuarter1txt.text forKey:#"totalOfTheQuater"];
[quarter setObject:showQuartertxt.text forKey:#"quarterP"];
[quarter setObject:[NSString stringWithFormat:#"%d",(int)slider4.value]forKey:#"quartersliderValue"];
}
QuarterValueArray =[[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"]mutableCopy];
Why this code is crashing when the method is called second time? The crash log says:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFDictionary setObject:forKey:]: mutating method sent to immutable object'

Try this.
NSMutableArray *quarterValueArray = [[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"] mutableCopy];
if (!quarterValueArray)
{
NSMutableArray *aQuarterArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 12; i++)
{
NSMutableDictionary *dict1 = [[[NSMutableDictionary alloc] init] mutableCopy];
[dict1 setObject:#"" forKey:#"quarterP"];
[dict1 setObject:#"" forKey:#"quartersliderValue"];
[dict1 setObject:#"" forKey:#"totalOfTheQuater"];
[aQuarterArray addObject:dict1];
}
[[NSUserDefaults standardUserDefaults] setValue:aQuarterArray forKey:#"QuarterValues"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[self saveUserDefaultDataQuarter];
NSLog(#"%#",quarterValueArray);
and modify like this.
-(void)saveUserDefaultDataQuarter{
NSMutableArray*QuarterValueArray =[[[NSUserDefaults standardUserDefaults]arrayForKey:#"QuarterValues"]mutableCopy];
NSMutableDictionary *quarter = [QuarterValueArray objectAtIndex:0];
[quarter setValue:totalQuarter1txt.text forKey:#"totalOfTheQuater"];
[quarter setValue:showQuartertxt.text forKey:#"quarterP"];
[quarter setValue:[NSString stringWithFormat:#"%d",(int)slider4.value]forKey:#"quartersliderValue"]; }
UPDATE :I have used set value for setting the dictionary key values, as dictionary is already created when we are modifying values.

Related

NSMutableArray isn't adding my NSString

Im trying to add a NSString to an NSMutableArray and then make the array into NSData and save it with NSUserDefaults. But the array is always nil.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
library = [[ALAssetsLibrary alloc] init];
groups = [[NSMutableArray alloc] init];
userDefaults = [[NSUserDefaults alloc] init];
NSData *data = [userDefaults objectForKey:#"GroupArray"];
groups = [NSKeyedUnarchiver unarchiveObjectWithData:data];
NSLog(#"%i", [groups count]);
}
-(IBAction)newFolder {
if (textField.text != nil) {
NSString *string = textField.text.capitalizedString;
[library addAssetsGroupAlbumWithName:string
resultBlock:^(ALAssetsGroup *group) {
NSLog(#"Created a folder named: %#", group);
}
failureBlock:^(NSError *error) {
NSLog(#"An error occured! - %#", error);
}
];
[groups addObject:string];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:groups];
[userDefaults setObject:data forKey:#"GroupArray"];
NSLog(#"%i", [groups count]);
}
[self.view sendSubviewToBack:subView];
}
When the app starts i get a message in the console that the array is NULL. When I try to add the string the NSLog(#"%i", [groups count]); always return 0.
Why does this happen?
userDefaults = [[NSUserDefaults alloc] init];
NSData *data = [userDefaults objectForKey:#"GroupArray"];
In this case, data will be nil when the code is executed for the first time, since there is yet no "GroupArray" property present.
groups = [NSKeyedUnarchiver unarchiveObjectWithData:data];
This causes groups be become nil as well, because calling unarchiveObjectWithData: with nil as an argument will return nil as well.
And because of all that, in -newFolder, [groups addObject:string] becomes [nil addObject:string]
Calling a method on nil is allowed in Objective-C, so you get no exception there. The return value of any method called on nil is, again, nil or 0.
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:groups];
This causes data to be nil because groups is nil.
So you are always calling:
[userDefaults setObject:data forKey:#"GroupArray"];
with data = nil
Quick fix:
Add if (groups == nil) groups = [NSMutableArray array]; to the beginning of -newFolder
In view didLoad method every time you are initialising new instance of NSUserDefaults and calling
NSData *data = [userDefaults objectForKey:#"GroupArray"]; on the newly created object
which will return nil all the time because in the new instance there wont be any object for key #"GroupArray". Instead replace userDefaults with singleton object [NSUserDefaults standardUserDefaults]
Modify your code as shown below
- (void)viewDidLoad
{
[super viewDidLoad];
library = [[ALAssetsLibrary alloc] init];
NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:#"GroupArray"];
groups = [NSKeyedUnarchiver unarchiveObjectWithData:data];
if (!groups)
{
groups = [[NSMutableArray alloc] init];
}
NSLog(#"group %#", groups);
NSLog(#"%i", [groups count]);
}
Your newFolder method
-(IBAction)newFolder
{
if (textField.text != nil)
{
NSString *string = textField.text.capitalizedString;
[library addAssetsGroupAlbumWithName:string
resultBlock:^(ALAssetsGroup *group) {
NSLog(#"Created a folder named: %#", group);
}
failureBlock:^(NSError *error) {
NSLog(#"An error occured! - %#", error);
}
];
[groups addObject:string];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:groups];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:#"GroupArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%i", [groups count]);
}
[self.view sendSubviewToBack:subView];
}
This should work. As it worked for me.

RestKit: mapping array of key-value pairs to NSDictionary

Webservice sends me JSON that looks like that:
[
{"key":"key1","value":"value1"},
{"key":"key2","value":"value2"},
{"key":"key3","value":"value3"}
]
How should I set up RestKit mapping to get the following NSDictionary?
#{ #"key1" : #"value1", #"key2" : #"value2", #"key3": #"value3" }
This may help you, try this.
NSDictionary *dict1 = [[NSMutableDictionary alloc] init];
NSArray *arr = [[NSArray alloc] initWithArray:<Parse the array here from RESTkit>];
for (int i = 0;i < [arr count], i++)
{
NSDictionary *dict = [arr objectAtIndex:i];
NSString *key = [dict objectForKey:#"key"];
NSString *value = [dict objectForKey:#"value"];
[dict1 setObject:value forKey:key];
}
NSLog(#" Dictionary %#", dict1);
[dict1 release];

NSMutableArray to NSUserDefults not saving

I am reading and writing an array to NSuserdefults in this way:
indexDelete = button.tag;
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:#"save"];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
myIntegers = [[NSMutableArray alloc] initWithArray:oldSavedArray];
else
myIntegers = [[NSMutableArray alloc] init];
}
[myIntegers addObject:[NSNumber numberWithInteger:indexDelete]];
NSLog (#"Array myIntegers: %#", myIntegers);
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myIntegers] forKey:#"save"];
Every thing works fine if I keep the line:
myIntegers = [[NSMutableArray alloc] init];
out of the brackets, because even if the data is there, it is not written or read correctly:
I get this error: -[NSKeyedUnarchiver initForReadingWithData:]: data is NULL
What im I doing wrong??
Change this:
[[NSUserDefaults standardUserDefaults] setObject:[NSKeyedArchiver archivedDataWithRootObject:myIntegers] forKey:#"save"];
to
[[NSUserDefaults standardUserDefaults] setObject:myIntegers forKey:#"save"];
if myIntegers will not be mutated (changed) in the future. If it will be changed then use:
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithArray:myIntegers] forKey:#"save"];
The ability to save arrays and dictionaries in the user defaults is documented in the class description.
You never initialize your myIntegers. The first time you get the object for key save from your defaults you get a nil object (hence not entering the if closure and allocating the structure). After that you try to archive a nil object, which is probably the cause for the error you're getting.
Try moving the initialization of the array before this and only adding the objects from the previous saving if they exist:
myIntegers = [[NSMutableArray alloc] init];
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
NSData *dataRepresentingSavedArray = [currentDefaults objectForKey:#"save"];
if (dataRepresentingSavedArray != nil)
{
NSArray *oldSavedArray = [NSKeyedUnarchiver unarchiveObjectWithData:dataRepresentingSavedArray];
if (oldSavedArray != nil)
[myIntegers addObjectsFromArray:oldSavedArray];
}
I have one alternative to do this is to save mutablearray in doc dir and then retrieve it:
// Get path to documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
if ([paths count] > 0)
{
// Path to save array data
NSString *arrayPath = [[paths objectAtIndex:0]
stringByAppendingPathComponent:#"array.out"];
// Write array
[yourMutableArray writeToFile:arrayPath atomically:YES];
// Read both back in new collections
NSMutableArray *arrayFromFile = [NSMutableArray arrayWithContentsOfFile:arrayPath];
for (NSString *element in arrayFromFile)
NSLog(#"Items: %#", element);
}

Removing objects from array in NSUserDefaults

When setting NSUserDefaults, I was initially using this code to set the defaults...
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"string1", #"string2", #"string3", nil];
[[NSUserDefaults standardUserDefaults] setObject:array forKey: #"preset1"];
[[NSUserDefaults standardUserDefaults] synchronize];
I've learned that I should be using this instead:
NSMutableArray *array = [NSMutableArray arrayWithObjects: #"string1", #"string2", #"string3", nil];
NSDictionary *appDefaults = [NSDictionary dictionaryWithObject:array forKey:#"preset1"];
[[NSUserDefaults standardUserDefaults] registerDefaults:appDefaults];
Now I'm having an issue manipulating the objects later on in array. Here is the code I use to add/remove strings from array. It worked fine when I was initially setting the defaults manually in my first example. Now, the objects will not remove from the array. I did notice when printing the array in LLDB debugger that array is now being stored as a NSCFArray when it was just an NSArray before.
NSMutableArray *array = [[NSMutableArray alloc] init];
[array addObjectsFromArray:[[NSUserDefaults standardUserDefaults] objectForKey:#"preset1"]];
NSArray *stringsToRemove = #[#"string1", #"string2" ];
for (NSUInteger i = 0; i < stringsToRemove.count; i++) {
[array removeObjectIdenticalTo:[stringsToRemove objectAtIndex:i]];
}
[[NSUserDefaults standardUserDefaults] setObject:array forKey: #"preset1"];
[[NSUserDefaults standardUserDefaults] synchronize];
This code works for me with your setup, after initializing the defaults the second way you described:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *array = [[NSMutableArray alloc] initWithArray: [defaults objectForKey:#"preset1"]];
NSArray *stringsToRemove = [NSArray arrayWithObjects:#"string1", #"string2", nil];
for (NSString *aString in stringsToRemove) {
[array removeObjectIdenticalTo:aString];
}
[defaults setObject:array forKey: #"preset1"];
[defaults synchronize];

Stopping a loop

As explained in my earlier question …
This code …
- (void)syncKVO:(id)sender {
NSManagedObjectContext *moc = [self managedObjectContext];
[syncButton setTitle:#"Syncing..."];
NSString *dateText = (#"Last Sync : %d", [NSDate date]);
[syncDate setStringValue:dateText];
NSEntityDescription *entityDescription = [NSEntityDescription
entityForName:#"projects" inManagedObjectContext:moc];
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:entityDescription];
NSError *error = nil;
NSArray *array = [moc executeFetchRequest:request error:&error];
if (array == nil)
{
NSAlert *anAlert = [NSAlert alertWithError:error];
[anAlert runModal];
}
NSArray *namesArray = [array valueForKey:#"name"];
NSPredicate *predicate = [CalCalendarStore taskPredicateWithCalendars:[[CalCalendarStore defaultCalendarStore] calendars]];
NSArray *tasksNo = [[CalCalendarStore defaultCalendarStore] tasksWithPredicate:predicate];
NSArray *tasks = [tasksNo valueForKey:#"title"];
NSMutableArray *namesNewArray = [NSMutableArray arrayWithArray:namesArray];
[namesNewArray removeObjectsInArray:tasks];
NSLog(#"%d", [namesNewArray count]);
NSInteger *popIndex = [calenderPopup indexOfSelectedItem];
//Load the array
CalCalendarStore *store = [CalCalendarStore defaultCalendarStore];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *supportDirectory = [paths objectAtIndex:0];
NSString *fileName = [supportDirectory stringByAppendingPathComponent:#"oldtasks.plist"];
NSMutableArray *oldTasks = [[NSMutableArray alloc] initWithContentsOfFile:fileName];
[oldTasks removeObjectsInArray:namesArray];
NSLog(#"%d",[oldTasks count]);
//Use the content
NSPredicate* taskPredicate = [CalCalendarStore taskPredicateWithCalendars:[[CalCalendarStore defaultCalendarStore] calendars]];
NSArray* allTasks = [[CalCalendarStore defaultCalendarStore] tasksWithPredicate:taskPredicate];
// Get the calendar
CalCalendar *calendar = [[store calendars] objectAtIndex:popIndex];
// Note: you can change which calendar you're adding to by changing the index or by
// using CalCalendarStore's -calendarWithUID: method
// Loop, adding tasks
for(NSString *title in namesNewArray) {
// Create task
CalTask *task = [CalTask task];
task.title = title;
task.calendar = calendar;
// Save task
if(![[CalCalendarStore defaultCalendarStore] saveTask:task error:&error]) {
NSLog(#"Error");
// Diagnostic error handling
NSAlert *anAlert = [NSAlert alertWithError:error];
[anAlert runModal];
}
}
NSMutableArray *tasksNewArray = [NSMutableArray arrayWithArray:tasks];
[tasksNewArray removeObjectsInArray:namesArray];
NSLog(#"%d", [tasksNewArray count]);
for(NSString *title in tasksNewArray) {
NSManagedObjectContext *moc = [self managedObjectContext];
JGManagedObject *theParent =
[NSEntityDescription insertNewObjectForEntityForName:#"projects"
inManagedObjectContext:moc];
[theParent setValue:nil forKey:#"parent"];
// This is where you add the title from the string array
[theParent setValue:title forKey:#"name"];
[theParent setValue:[NSNumber numberWithInt:0] forKey:#"position"];
}
for(CalTask* task in allTasks)
if([oldTasks containsObject:task.title]) {
[store removeTask:task error:nil];
}
// Create a predicate for an array of names.
NSPredicate *mocPredicate = [NSPredicate predicateWithFormat:#"name IN %#", oldTasks];
[request setPredicate:mocPredicate];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
// Execute the fetch request put the results into array
NSArray *resultArray = [moc executeFetchRequest:request error:&error];
if (resultArray == nil)
{
// Diagnostic error handling
NSAlert *anAlert = [NSAlert alertWithError:error];
[anAlert runModal];
}
// Enumerate through the array deleting each object.
// WARNING, this will delete everything in the array, so you may want to put more checks in before doing this.
for (JGManagedObject *objectToDelete in resultArray ) {
// Delete the object.
[moc deleteObject:objectToDelete];
}
//Save the array
[namesArray writeToFile:fileName atomically:YES];
[syncButton setTitle:#"Sync Now"];
NSLog(#"Sync Completed");
}
triggers this code …
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:#"name"]) {
[self performSelector:#selector(syncKVO:)];
}
}
because I am adding objects and the KVO method is triggered when the Core Data 'name' property is changed.
I need to stop the observeValueForKeyPath:ofObject:change:context: method if it was triggered by the syncKVO method. How would I do this?
The simplest thing you could do is use an instance variable which keeps track of whether you’re syncing and ignore observer changes when it’s set. It may be better to stop and start observing at the beginning and end of syncKVO:, but it depends on what you’re actually observing: you don’t want to mass unsubscribe and resubscribe if you’re watching a large collection.
Looking at your code I wonder if you really want to do this syncing when entities are saved, and not as soon as the object keys changed. I think you’d be better off ditching observing completely and watching for the NSManagedObjectContextObjectsDidChangeNotification, using the values of the userInfo keys specified in the CoreData documentation to determine which entities need to be updated.