How can we store into an NSDictionary? What is the difference between NSDictionary and NSMutableDictionary?

The NSDictionary and NSMutableDictionary docs are probably your best bet. They even have some great examples on how to do various things, like… …create an NSDictionary NSArray *keys = [NSArray arrayWithObjects:@”key1″, @”key2″, nil]; NSArray *objects = [NSArray arrayWithObjects:@”value1″, @”value2″, nil]; NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys]; …iterate over it for (id key in dictionary) { … Read more

Swift equivalent to `[NSDictionary initWithObjects: forKeys:]`

As of Swift 4 you can create a dictionary directly from a sequence of key/value pairs: let keys = [“one”, “two”, “three”] let values = [1, 2, 3] let dict = Dictionary(uniqueKeysWithValues: zip(keys, values)) print(dict) // [“one”: 1, “three”: 3, “two”: 2] This assumes that all keys are different, otherwise it will abort with a … Read more

how to do true deep copy for NSArray and NSDictionary with have nested arrays/dictionary?

A couple of years ago, I wrote a few category methods for exactly the same reason, transforming a whole tree of user defaults to mutable. Here they are – use them at your own risk! 🙂 // // SPDeepCopy.h // // Created by Sherm Pendley on 3/15/09. // #import <Cocoa/Cocoa.h> // Deep -copy and -mutableCopy … Read more

Sorting NSArray of dictionaries by value of a key in the dictionaries

I think this will do it: brandDescriptor = [[NSSortDescriptor alloc] initWithKey:@”brand” ascending:YES]; sortDescriptors = [NSArray arrayWithObject:brandDescriptor]; sortedArray = [myArray sortedArrayUsingDescriptors:sortDescriptors]; I pulled the code from Sort Descriptor Programming Topics. Also, Key-Value Coding comes into play, in that sortedArrayUsingDescriptors: will send a valueForKey: to each element in myArray, and then use standard comparators to sort the … Read more

How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)

It looks like you are passing an NSString parameter where you should be passing an NSData parameter: NSError *jsonError; NSData *objectData = [@”{\”2\”:\”3\”}” dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError];

Best way to sort an NSArray of NSDictionary objects?

Use NSSortDescriptor like this.. NSSortDescriptor * descriptor = [[NSSortDescriptor alloc] initWithKey:@”interest” ascending:YES]; stories = [stories sortedArrayUsingDescriptors:@[descriptor]]; recent = [stories copy]; stories is the array you want to sort. recent is another mutable array which has sorted dictionary values. Change the @”interest” with the key value on which you have to sort. All the best

Using NSPredicate to filter an NSArray based on NSDictionary keys

It should work – as long as the data variable is actually an array containing a dictionary with the key SPORT NSArray *data = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@”foo” forKey:@”BAR”]]; NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@”(BAR == %@)”, @”foo”]]; Filtered in this case contains the dictionary. (the %@ does not have to be quoted, this is done … Read more

Swift – Stored values order is completely changed in Dictionary

This is because of the definition of Dictionaries: Dictionary A dictionary stores associations between keys of the same type and values of the same type in an collection with no defined ordering. There is no order, they might come out differently than they were put in. This is comparable to NSSet. Edit: NSDictionary Dictionaries Collect … Read more

Best practice? – Array/Dictionary as a Core Data Entity Attribute [closed]

There is no “native” array or dictionary type in Core Data. You can store an NSArray or an NSDictionary as a transformable attribute. This will use the NSCoding to serialize the array or dictionary to an NSData attribute (and appropriately deserialize it upon access). The advantage of this approach is that it’s easy. The downside … Read more