O'Reilly logo

iOS 6 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

20.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 1" : "value 1",
  "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"
  	}
  ]
}

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"
    },
    @

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required