一尘不染

如何在Swift中将Realm对象转换为JSON?

swift

我声明了两个Realm表:

class Task: Object {
    dynamic var taskID: String = ""
    let taskAssignedTo = List<Contacts>()
}

class Contacts: Object {
    dynamic var contactEmail: String = ""
    dynamic var contactName: String = ""
}

最终目标是将Task Realm对象转换为JSON。我在想的方法是:

使用类中的方法将对象转换为字典

func taskToDictionary() -> [String: AnyObject] {
    return [
        "taskID" : self.taskID,
        "taskAssignedTo" : self.taskAssignedTo._rlmArray.count //Not sure how to get the array
    ]
}

使用SwiftyJSON将结果字典转换为JSON

let taskObject = Task()
let newTaskJSON = JSON(taskObject.taskToDictionary())

现在,这可以转换,但是:

  1. 有一个更好的方法吗?
  2. 如何将RLMArray转换为数组以进行JSON转换?

阅读 385

收藏
2020-07-07

共1个答案

一尘不染

设法在这里找到答案:

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

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

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

    for prop in self.objectSchema.properties as [Property]! {
        // find lists
        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
}
}

Xcode 7和Swift 2的更新:

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

    let mutabledic = NSMutableDictionary()
    mutabledic.setValuesForKeysWithDictionary(dictionary)

    for prop in self.objectSchema.properties as [Property]! {
        // find lists
        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  {
                let object = nestedListObject._rlmArray[index] as AnyObject
                objects.append(object.toDictionary())
            }
            mutabledic.setObject(objects, forKey: prop.name)
        }

    }
    return mutabledic
}

}

更新到Xcode 8和Swift 3:

extension Object {
func toDictionary() -> NSDictionary {
    let properties = self.objectSchema.properties.map { $0.name }
    let dictionary = self.dictionaryWithValues(forKeys: properties)
    let mutabledic = NSMutableDictionary()
    mutabledic.setValuesForKeys(dictionary)

    for prop in self.objectSchema.properties as [Property]! {
        // find lists
        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  {
                let object = nestedListObject._rlmArray[index] as AnyObject
                objects.append(object.toDictionary())
            }
            mutabledic.setObject(objects, forKey: prop.name as NSCopying)
        }
    }
    return mutabledic
}
}
2020-07-07