Data Conversion in the SDK

This topic describes how the data formats used with key-value pairs are converted when the Kii Cloud SDK reads key-value pairs.

You can skip this topic if you are using only one platform (e.g. your application is only released for iOS) and if your application does not have any data format casting (e.g. all values written as "int" are read as "int").

The Kii Cloud SDK for iOS provides separate APIs for reading data of a basic type such as NSString and NSNumber and that of the GeoPoint (geolocation) type. There are dedicated methods for setting and getting a GeoPoint object. This topic explains how to get data of any of the other data types with the getForKey(_:) method.

The getForKey(_:) method gets a value from a JSON document and returns it as a value of the id type. A value of the id type is cast to a specific data type such as NSString, NSNumber, NSArray, and NSDictionary according to its data type in the JSON document. Unlike on the other platforms such as Android, the SDK for iOS does not throw an error when reading data because it does not forcibly read data assuming a certain data type.

You can distinguish between a case that a specified key does not exist and a case that a specified key has a null value. Nil is returned for the former case and NSNull is returned for the latter case.

Example of data conversion

Suppose the following JSON document is stored in a KiiObject. The next table shows how the getForKey(_:) method processes the key-value pairs.

  "value1": "abc",
  "value2": "123",
  "value3": "123x456",
  "value4": {"a":"b"},
  "value5": null,
  "value6": [1, 2, 3],
  "value7": 123,
  "value8": 8589934592,
  "value9": 456.789,
  "value10": true
Key Value A return from getForKey(_:) Notes
value1 "abc" abc (NSString)
value2 "123" 123 (NSString)
value3 "123x456" 123x456 (NSString)
value4 {"a":"b"} {a=b} (NSDictionary)
value5 null NSNull
value6 [1,2,3] {1,2,3} (NSArray)
value7 123 123 (NSNumber) As the value for the intValue property
value8 8589934592 8589934592 (NSNumber) As the value for the int64Value property
value9 456.789 456.789 (NSNumber) As the value for the doubleValue property
value10 true YES (NSNumber) As the value for the boolValue property
Non-existing key n/a nil