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

Working with Boxed Expressions In Objective-C

Boxed expressions allow values to be interpreted before assigning to an object when using literals with Objective-C (see the end of post for links to more information). It’s not as confusing as it sounds, let’s look at a few examples:

The code below shows how to used a boxed expression to create a NSNumber object, calculated by multiplying to values:

Continue reading

List Files In A Directory And All Subdirectores

Following is a few lines of Objective-C code to quickly traverse a directory and all of its subdirectories.

Let’s start with a file structure in the Documents directory that I used for my example:

The example I am writing is using the simulator, thus the folder structure shown above is from my Mac. However, the same applies regardless of the platform.
Continue reading

NSDictionary Literals In Objective-C

I’ve previously written about NSNumber and NSArray literals, let’s have a look at NSDictionary literals.

Before we get started, a quick note, to use literals you will need to be running Xcode 4.4 or greater as well as the Apple LLVM compiler 4.0 or greater.

As with NSString objects, which allow definition using literal syntax:

you can now create NSDictionary objects using literals. A range of examples follow, showing the old style definition as well as the new literal syntax:
Continue reading

NSNumber Literals In Objective-C

Last week I wrote about NSArray Literals in Objective-C. This week I will show you how to use NSNumber literals. To use literals, you will need to be running Xcode 4.4 or greater as well as the Apple LLVM compiler 4.0 or greater.

Just as a string literal is prefixed with an ‘@’ symbol:

you can now create NSNumber objects using literals. A range of examples follow, showing the old style definition as well as the new literal syntax:
Continue reading