Read and Write NSArray, NSDictionary and NSSet to a File

With just a few lines of code, you can read/write collections to/from files. The code below shows examples for writing and reading both NSArray and NSDictionary objects, the same logic would apply to an NSSet (or other collection type).

The example below starts by populating both an array and dictionary, each using the Objective-C literal syntax. Read more about using NSArray literals and NSDictionary literals.

Continue reading

NSArray Literals In Objective-C

When running Xcode 4.4 (the latest download from the Mac App Store) I noticed the option for Apple LLVM Compiler 4.0:

One reason this is significant is that you can now define literals for a number of additional objects, similar to how you define string literals with the @ prefix. In this post I’ll show you how to use NSArrays literals. Let’s look at a few examples:

// Previous format to define empty array
NSArray *emptyArray = [NSArray array];
// Previous format to define single element array
NSArray *singleElementArray = [NSArray arrayWithObject:@"Error Message"];
// Previous format to define array of strings
NSArray *arrayOfStrings = [NSArray arrayWithObjects: @"First Name", @"Last Name", nil];

The new NSArray literal format follows:
Continue reading