gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-10-31 23:56:31 26 4
gpt4 key购买 nike

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

最佳答案

设法在这里找到答案:

Can I serialize a RealmObject to JSON or to NSDictionary in Realm for Swift?

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
}
}

关于json - 如何在 Swift 中将 Realm 对象转换为 JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32023249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com