Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

Tuesday, October 06, 2015

JSON: A 5-Minute Introduction

JSON is getting popular by the days. JSON stands for JavaScript Object Notation. It is used in data representation as well as data transmission.

To very if a string is a valid JSON string, use the JSONLint at http://jsonlint.com

JSON Data Types

JSON supports the following data types:
  • Object
  • String
  • Boolean
  • Number
  • Array
  • null

The following sections will elaborate on each of the data types.

Object


An Object is an unordered collection of key:value pairs enclosed in a pair of curly braces ({}). The following is an example of an empty object:

{}

String

The key in an object must be a String, while the value can be either a String, Boolean, Number, Array, null, or another Object.

The following shows an object with one key:value pair:

{
    "firstName": "John"
}

An object can have multiple key:value pairs, for example:

{
    "firstName": "John",
    "lastName": "Doe"
}

Note the comma (,) after John and there is no comma after Doe.

Each key in the Object must be unique. For example, the following example is not a valid JSON string:

{
    "firstName": "John",
    "firstName": "Doe"
}

Boolean

A Boolean value can either be true or false:

{
    "firstName": "John",
    "lastName": "Doe",
    "isMember": true
}

Number

A Number value can either be an integer, or a floating-point number:

{
    "firstName": "John",
    "lastName": "Doe",
    "isMember": true,
    "weight": 79.5,
    "height": 1.73,
    "children": 3
}

Nested Object

The value of a key can also be another Object, as the following example shows:

{
    "firstName": "John",
    "lastName": "Doe",
    "isMember": true,
    "weight": 79.5,
    "height": 1.73,
    "children": 3,
    "address": {
        "line1": "123 Street",
        "line2": "San Francisco",
        "state": "CA",
        "postal": "12345"
    }
}

Array

An Array is an ordered sequence of Objects:

{
    "firstName": "John",
    "lastName": "Doe",
    "isMember": true,
    "weight": 79.5,
    "height": 1.73,
    "children": 3,
    "address": {
        "line1": "123 Street",
        "line2": "San Francisco",
        "state": "CA",
        "postal": "12345"
    },
    "phone": [
        {
            "type": "work",
            "number": "1234567"
        },
        {
            "type": "home",
            "number": "8765432"
        },
        {
            "type": "mobile",
            "number": "1234876"
        }
    ]
}

Note that arrays are denoted with a pair of brackets ([]).

null

When a key has no value, you can assign a null to it:

{
    "firstName": "John",
    "lastName": "Doe",
    "isMember": true,
    "weight": 79.5,
    "height": 1.73,
    "children": 3,
    "address": {
        "line1": "123 Street",
        "line2": "San Francisco",
        "state": "CA",
        "postal": "12345"
    },
    "phone": [
        {
            "type": "work",
            "number": "1234567"
        },
        {
            "type": "home",
            "number": "8765432"
        },
        {
            "type": "mobile",
            "number": "1234876"
        }
    ],
    "oldMembershipNo": null

}

Saturday, September 05, 2015

Tip - Swift 2.0 - Converting Keys and Values in a Dictionary into Arrays

Very often, you need to extract keys and values stored within a dictionary as arrays. Here's how to do it:

//---declare a dictionary---
var stocks = Dictionary<String, Double>()

//---populate the dictionary---
stocks["AAPL"] = 109.15
stocks["MSFT"] = 42.97
stocks["GOOG"] = 603.00

//---extract the keys as an array---
let symbols = Array(stocks.keys) //["AAPL", "GOOG", "MSFT"]

//---extract the values as an array---
let prices = Array(stocks.values) //[109.15, 603, 42.97]

Friday, August 02, 2013

iOS Tip: Specifying Hardware Requirements

If your application uses special hardware (such as front camera) in order to work, you need to specify that in your info.plist file. Failure to do so will likely anger users who have downloaded (or even paid) for your application only to realize that the app does not work on their devices because they don’t have the correct hardware. Specifying the hardware requirements thus prevents users who do not have the appropriate hardware (as required by your app) from being to purchase your app. But what about specifying hardware that the users should not have? For example, you only want your app to run on the iPod touch and iPad. Hence you need to specify that the device must not have the telephony feature.

To specify the hardware required by your app, you simply need to specify the hardware features in the info.plist file in your project:


However, by default the Property List editor only allows you to set the Required device capabilities key (the raw key name is UIRequiredDeviceCapabilities ) as an array type. This means that you can only set the hardware required by your app, and not the features that you want to exclude. In the above example, your app will require a device running on armv7, has telephony feature (i.e. iPhones), and has a front-facingcamera.

If you want to specify feature(s) that your app do not need, you need to represent the UIRequiredDeviceCapabilities as a dictionary type.

This is how you do it. Right-click on the -info.plist file and select Open As | Source Code. This will show the .plist file as an XML file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleDisplayName</key>
    <string>${PRODUCT_NAME}</string>
    <key>CFBundleExecutable</key>
    <string>${EXECUTABLE_NAME}</string>
    <key>CFBundleIdentifier</key>
    <string>net.learn2develop.${PRODUCT_NAME:rfc1034identifier}
</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>${PRODUCT_NAME}</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
        <string>telephony</string>
        <string>front-facing-camera</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
</dict>
</plist>
As you can see, our three requirements are specified in the element, each represented by a element. To change the UIRequiredDeviceCapabilities to the dictionary type, add in the following statements in bold:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleDisplayName</key>
    <string>${PRODUCT_NAME}</string>
    <key>CFBundleExecutable</key>
    <string>${EXECUTABLE_NAME}</string>
    <key>CFBundleIdentifier</key>
    <string>net.learn2develop.${PRODUCT_NAME:rfc1034identifier}
</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>${PRODUCT_NAME}</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>1.0</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UIRequiredDeviceCapabilities</key>
    <dict>
        <key>armv7</key>
        <true/>
        <key>telephony</key>
        <false/>
        <key>front-facing-camera</key>
        <true/>
    </dict>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
</dict>
</plist>

In the example above, we specified that the telephony feature as false. That is, our app can only run on an iPod touch,  iPad, or iPad mini.

If you now right-click the info.plist file and select Open As | Property List, you will see the following: