Category Archives: Cocoa

Saving NSArray in iOS

This is quick and dirty post. It’s more of a bookmark for me, because I find toggling between C# and Objective C makes meĀ  terrible with Objective C if I am away from it for a month or so.

The Requirement

  • Save a list of records on the iPhone quickly and retrieve them later as required.

The Approach

Use the NSKeyedArchiver to save and load arrays of data.


Setting up a file name and path to save save

// need a path
- (NSString *) getPath
  NSString* path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  return [path stringByAppendingPathComponent:@"MyFile.dat"];

Save the data

// save an array
- (void) saveSettingsData: (NSArray*)data
  [NSKeyedArchiver archiveRootObject:data toFile:[self getPath]];

Load the data

// get that array back
- (NSArray *) loadSavedData
  return [NSKeyedUnarchiver unarchiveObjectWithFile:[self getPath]];

That’s about it. I have used it for saving an array of dictionary objects and it works just fine. I have not tried it for array of custom entities. Will update here as I try that. If that doesn’t work I have go deeper into the ManageObjectContext and PersistentStoreCoordinator and of course blog about it.

UPDATE: Well that didn’t take long. Just confirmed that you can only persist NSArray objects containing other NSArray or NSDictionary objects. You cannot serialize an NSArray instance of custom Entities. So the next article on ManageObjectContext is now on.

Tagged , , ,

Sorting an NSArray of NSDictionary objects, based on values in NSDictionary

How to use sortedArrayUsingFunction

Today I came across a requirement where I had to Sort an NSArray. Each element of the array had a NSDictionary which had only one element, which was a NSArray. Structure was like:



—->—->”somekey1″ – NSArray [val1, val2, val3… val-n]


—->—->”somekey2″ – NSArray [val1, val2, val3… val-m]


—->—->”somekey3″ – NSArray [val1, val2, val3… val-o]




—->—->”somekey-x” – NSArray [val1, val2, val3… val-w]

(where n!=m!=o!=w)

I wanted this NSArray to be sorted so that the NSDictionary with the largest number of NSArray values in it should come on sorted.

After looking at the available options in Apple’s Documentation here I decided to define a selector (‘delegate’ for other .NET folks like me) and provide the selector to the sortedArrayUsingFunction method available for an NSArray.

The sorting function looks like this

NSInteger sortByArrayCount(id dictionary1, id dictionary2, void *reverse)

    NSMutableDictionary *dict1 = (NSMutableDictionary *)dictionary1;
    NSMutableDictionary *dict2 = (NSMutableDictionary *)dictionary2;

    NSArray *arr1 = (NSArray *)[[dict1 objectEnumerator] nextObject];
    NSArray *arr2 = (NSArray *)[[dict2 objectEnumerator] nextObject];

    NSLog(@"Count of Array 1: %d, Array 2: %d", [arr1 count], [arr2 count]);
    if(*(BOOL *)reverse == YES)
        return ([arr2 count] > [arr1 count]);
        return ([arr1 count] > [arr2 count]);

I think the code is quite self explanatory, we convert the values passed into the method. Then extract the first element in the dictionary as NSArray, finally I check if reverse==YES is passed or not this flag indicates whether you want a Descending sort or not. Finally do the comparison.

To sort an array you call it as follows:

NSArray *sortedArray;
BOOL reverseSort = NO;
sortedArray = [catList sortedArrayUsingFunction:sortByArrayCount context:&reverseSort];

Point to note here is once you have got the NSDictionary you could make the sorting condition on any of the values in the dictionary. For example string compare the Key of the two inputs.

The sortArrayByFunction method offers a lot of flexibility in terms of deciding how you want to sort. My case was a unique one.

Other options to explore are ofcourse using sortDescriptors if you wanted to sort based on string values in NSArray of NSDictionaries.

That’s about it for now. Have fun.

(Yay! to my first iOS article)

Tagged , , ,
%d bloggers like this: