In order to set a key-value pair in a KiiObject, call the setObject(_:forKey:) method of the KiiObject class. The setObject(_:forKey:) method has an overloaded version for each data type.
Specify a key-value pair as arguments of the setObject(_:forKey:) method. The specified key-value pair will be saved at the first level of the JSON document hierarchy.
Data in the JSON format will be created as below after executing the above code and saving the KiiObject (Predefined fields are omitted).
{"score":987,"premiumUser":false,"mode":"easy"}
In order to set a key-value pair at the second or subsequent level, prepare a JSON object that has the key-value pair at the desired level and set the JSON object at the first level.
The maximum size of a KiiObject is 65534 characters (the total size of key-value pairs in the JSON format, including some internal fields used by Kii Cloud).
Supported data types
You can use any of NSString, NSNumber, NSArray, and NSDictionary for setting a value with the setObject(_:forKey:) method.
Kii Cloud does not have any limitation to the ranges of stored values and can store the maximum values for the data types supported by NSNumber. The following table includes a subset of supported data types by NSNumber. You can use the other data types such as Int16 and Float.
Use the values in the "Stored key-value pair in the JSON format" column in the following table as reference when checking data in the data browser in the developer portal and referencing data from another platform. You do not need to pay attention to the JSON format if you to use the same data type for writing and reading a key-value pair.
You cannot set a null value. An attempt to set a null value will be ignored or cause an error.
You can easily handle a date value (NSDate) by storing the number of milliseconds since 00:00:00 UTC, Thursday, 1 January 1970. To do so, multiply a Double value in the timeIntervalSince1970 property by 1000.
Unlike the Android SDK, the iOS SDK does not support the feature to encode a byte array as BASE64.
Setting a value of a basic data type
The following sample code illustrates how to set NSString, Int, Double, and Bool values when a KiiObject is created.
This is based on the sample code in Creating a KiiObject. The code block for setting values is different.
Swift:
Objective-C:
The above sample code creates a KiiObject with the following JSON format data (Predefined fields are omitted).
This section explains how to set a geolocation (KiiGeoPint) in a KiiObject.
First, create a KiiGeoPoint object by passing a latitude and a longitude. Then, set a key-value pair that has the KiiGeoPoint object as its value in the KiiObject with the setGeoPoint(_:forKey:) method.
The following sample code illustrates how to create a KiiObject in "MyBucket", a bucket in the currently logged-in user's scope, and set two geolocations in the KiiObject.
Swift:
Objective-C:
Make sure to call the save(_:) method. The key-value pairs set in the KiiObject are not saved on Kii Cloud until the method is called.
Setting complex data
In addition to the basic data types, the SDK supports a byte array, a JSON object, and an array of JSON objects. They correspond to the NSDictionary and NSArray, respectively.
The following sample code illustrates how to set values of these data types.
Swift:
Objective-C:
Make sure to call the save(_:) method. The key-value pairs set in the KiiObject are not saved on Kii Cloud until the method is called.
The above sample code creates a KiiObject with the following JSON format data (Predefined fields are omitted).