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

我声明了两个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转换?

回答:

设法在这里找到答案:

我可以在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

}

}

回答:

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

}

}

回答:

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

}

}

以上是 如何在Swift中将Realm对象转换为JSON? 的全部内容, 来源链接: utcz.com/qa/404285.html

回到顶部