我正在测试Realm,但是找不到将对象转换为JSON的简单方法。我需要将数据推送到我的REST接口。我该如何使用swift?
class Dog: Object {
dynamic var name = ""
}
class Person : Object {
dynamic var name = ""
let dogs = List<Dog>()
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试类似的操作,但无法迭代未知对象(列表)
extension Object {
func toDictionary() -> NSDictionary {
let props = self.objectSchema.properties.map { $0.name }
var dicProps = self.dictionaryWithValuesForKeys(props)
var mutabledic = NSMutableDictionary()
mutabledic.setValuesForKeysWithDictionary(dicProps)
for prop in self.objectSchema.properties as [Property]! {
if let objectClassName = prop.objectClassName {
if let x = self[prop.name] as? Object {
mutabledic.setValue(x.toDictionary(), forKey: prop.name)
} else {
//problem here! …Run Code Online (Sandbox Code Playgroud)