23.2. Creating Pass Files

Problem

You want to create a pass file that represents the data that you want your users to hold in their iOS devices.

Solution

Create a pass.json file and populate it with appropriate keys and values.

Discussion

Apple has chosen JSON files to represent passes for Pass Kit. JSON stands for JavaScript Object Notation and is extensively used in web applications and services. However, as an iOS developer, you don’t necessarily have to know about JSON files.

JSON files are simple key-value files, just like a dictionary. A key can have a value and the value can range from a simple string to a dictionary that contains keys and values itself. Here is a simple JSON that will pretty much demonstrate all there is to know about JSON files:

{
    "key 2 - dictionary" =     {
        "key 2.1" = "value 2.1";
        "key 2.2" = "value 2.2";
    };
    "key 3 - array" =     (
                {
            "array item 1, key1" = value;
            "array item 1, key2" = value;
        },
                {
            "array item 2, key1" = value;
            "array item 2, key2" = value;
        }
    );
    key1 = value1;
}

You can see that dictionaries are represented with square brackets and arrays with curly brackets. Other values are just simple key value pairs. If we were to represent this same JSON object with a normal NSDictionary, this is what the resulting code would be:

NSDictionary *json = @{ @"key1" : @"value1", @"key 2 - dictionary" : @{ @"key 2.1" : @"value 2.1", @"key 2.2" : @"value 2.2", }, @"key 3 - array" : @[ @{ @"array item 1, key1" : @"value", @"array item 1, key2" : @"value" }, @{ @"array item ...

Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.