is it possible to save NSMutableArray or NSDictionary data as file in iOS?

To write in file NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; [myArray writeToFile:filePath atomically:YES]; To read from file NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *filePath = [documentsDirectory stringByAppendingPathComponent:FILE_NAME]; myArray = [NSArray arrayWithContentsOfFile:filePath]; myArray will be nil if file is not … Read more

for each loop in Objective-C for accessing NSMutable dictionary

for (NSString* key in xyz) { id value = xyz[key]; // do stuff } This works for every class that conforms to the NSFastEnumeration protocol (available on 10.5+ and iOS), though NSDictionary is one of the few collections which lets you enumerate keys instead of values. I suggest you read about fast enumeration in the … Read more

Serialize and Deserialize Objective-C objects into JSON

Finally we can solve this problem easily using JSONModel. This is the best method so far. JSONModel is a library that generically serialize/deserialize your object based on Class. You can even use non-nsobject based for property like int, short and float. It can also cater nested-complex JSON. Considering this JSON example: { “accounting” : [{ … 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

What is an NSCFDictionary?

NSDictionary and the other collection classes are actually class clusters: several concrete subclasses classes masquerading under the interface of a single class: they all provide the same functionality (because they are subclasses of the same class — in NSDictionary’s case, this involves the three “primitive methods” -count, -objectForKey:, and -keyEnumerator), but have different internal workings … Read more

Replace all NSNull objects in an NSDictionary

I’ve made a few changes to Jacob’s original answer to extend it to handle dictionaries and arrays stored within the original dictionary. #import “NSDictionary+NullReplacement.h” #import “NSArray+NullReplacement.h” @implementation NSDictionary (NullReplacement) – (NSDictionary *)dictionaryByReplacingNullsWithBlanks { const NSMutableDictionary *replaced = [self mutableCopy]; const id nul = [NSNull null]; const NSString *blank = @””; for (NSString *key in self) … Read more