我可以在Realm for Swift中将RealmObject序列化为JSON或NSDictionary吗?

时间:2015-06-30 23:15:15

标签: ios swift realm

我正在测试Realm,但我找不到将对象转换为JSON的简单方法。 我需要将数据推送到我的REST界面。 我怎么能用swift来做呢?

class Dog: Object {
  dynamic var name = ""
}

class Person : Object {
  dynamic var name = ""
  let dogs = List<Dog>()
}

我正在尝试这样的事情,但我不能迭代未知对象(List)

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!
        }
      }
    }
    return mutabledic
  }
}

**抱歉丑陋的代码。

3 个答案:

答案 0 :(得分:4)

我也是Realm的新手,但我认为最简单的方法是反思Object's schema

class Person: Object {
    dynamic var name = ""
    dynamic var age = 0
}

let person = Person()

let schema = person.objectSchema

let properties = schema.properties.map() { $0.name }

let dictionary = person.dictionaryWithValuesForKeys(properties) // NSDictionary

println(properties)
println(dictionary)

答案 1 :(得分:2)

我认为我找到了解决方案。 我不依赖于表现。

extension Object {
  func toDictionary() -> NSDictionary {
    let properties = self.objectSchema.properties.map { $0.name }
    let dicProps = self.dictionaryWithValuesForKeys(properties)

    var mutabledic = NSMutableDictionary()
    mutabledic.setValuesForKeysWithDictionary(dicProps)

    for prop in self.objectSchema.properties as [Property]! {

      if let objectClassName = prop.objectClassName  {
        if let nestedObject = self[prop.name] as? Object {
          mutabledic.setValue(nestedObject.toDictionary(), forKey: prop.name)
        } else if let nestedListObject = self[prop.name] as? ListBase {
            var objects = [AnyObject]()
            for index in 0..<nestedListObject._rlmArray.count  {
              if let object = nestedListObject._rlmArray[index] as? Object {
                objects.append(object.toDictionary())
              }
            }
            mutabledic.setObject(objects, forKey: prop.name)
        }
      }
    }
    return mutabledic
  }
}

答案 2 :(得分:2)

这是我的解决方案。使用unsafeBitCast来避免强制转换失败警告。

extension Object {
    func toDictionary() -> [String:AnyObject] {
        let properties = self.objectSchema.properties.map { $0.name }
        var dicProps = [String:AnyObject]()
        for (key, value) in self.dictionaryWithValuesForKeys(properties) {
            if let value = value as? ListBase {
                dicProps[key] = value.toArray()
            } else if let value = value as? Object {
                dicProps[key] = value.toDictionary()
            } else {
                dicProps[key] = value
            }
        }
        return dicProps
    }
}

extension ListBase {
    func toArray() -> [AnyObject] {
        var _toArray = [AnyObject]()
        for i in 0..<self._rlmArray.count {
            let obj = unsafeBitCast(self._rlmArray[i], Object.self)
            _toArray.append(obj.toDictionary())
        }
        return _toArray
    }
}