How to save an Array of Custom Objects as value of a key in a document--- for CBL iOS , swift 3

mobile

#1

I have an array of custom objects (subclass of NSObject), which grows over time as users append more such custom objects to it. I’d like to save this growing array as value of a key in CBL document. Simply putting this array as value as following doesn’t work (gives internal error):

let properties: Dictionary<String, Any> = [

keyOfArray: arrayOfCustomObject,

]
//assuming doc is created.
doc.putProerties(properties)

I was wondering how should I implement this? If sample code for this kind of operation can be provided, that would be greatly appreciated ! Many Thanks !

p.s. the part of podfile related to CBL:
pod ‘couchbase-lite-ios’, ‘~> 1.4.1’


#2

CBL only stores types that can be encoded as JSON — boolean, numbers, strings, arrays, dictionaries. You’ll need to come up with a way to translate your objects to those types (usually to a dictionary.)


#3

Thanks Jens. arrays are in the list you gave, what kind of arrays does it refers to? arrays of primitive types only?

I can surely convert each object into a dictionary, but does CBL allow me to store an array of such dictionaries as value of a key?


#4

The types are defined by the JSON format; you may want to read up on that first. Arrays and dictionaries can recursively contain any of the types as values. (But dictionary keys have to be strings.)


#5

Thanks Jens. Problem solved !