我在我的项目中同时使用了 ObjectMapper ( https://github.com/Hearst-DD/ObjectMapper ) 和 Realm。 我的对象都是RLMObjects;
例如,我有一个包含附件的博客对象:
dynamic var attachments = RLMArray(objectClassName: Attachment.className())
我有一个像这样开始的自定义转换器:
func transformFromJSON(value: AnyObject?) -> RLMArray? {
let attachments = RLMArray(objectClassName: Attachment.className())
if let str = value as? String {
我似乎无法理解如何将它们转换为 RLMArray,我的转换器中总是得到 nil。
"attachments" : [
{
"id" : 2,
"file_name" : "img1.jpg",
"url" : "uploads\/img.jpg"
},
{
"id" : 3,
"file_name" : "img1.jpg",
"url" : "uploads\/img.jpg"
},
{
"id" : 4,
"file_name" : "img1.jpg",
"url" : "uploads\/img.jpg"
}
],
我发现很难全神贯注于代码,而且我在 xcode 的调试器中也找不到太多帮助。
TL;DR 我的转换器收到一个 nil,或者我期望如何将值转换为 RLMArray 的类型错误。
更新: 我希望它能有点清楚我正在努力解决的问题。 无论如何,为了调试,我改变了我的变压器以包含它:
func transformFromJSON(value: AnyObject?) -> RLMArray? {
let attachments = RLMArray(objectClassName: Attachment.className())
if let val:AnyObject = value {
Debug.log("the val is this \(val)")
Debug.log("the val is this \(val as? String)")
Debug.log("the val is this \(val as? Attachment)")
Debug.log("the val is this \(val as? Array<Attachment>)")
Debug.log("the val is this \(val as? Array<String>)")
Debug.log("the val is this \(val as? Dictionary<String, AnyObject>)")
Debug.log("the val is this \(val as? Dictionary<String, String>)")
let mir = reflect(value)
Debug.log("the mirror is this \(mir)")
}
return attachments
}
更令人困惑的是第一个 Debug.log(它只是一个 NSLog)。输出如下:
the val is this (
{
"file_name" = "img.jpg";
id = 2;
url = "uploads/img.jpg";
},
{
"file_name" = "img2.jpg";
id = 3;
url = "uploads/img2.jpg";
},
{
"file_name" = "img3.jpg";
id = 4;
url = "uploads/img3.jpg";
}
)
据我所知,这既不是 json,也不是数组或字典,因为这些日志行返回:
val 是这个 nil 或者,对于一个数组,val 是这个 Optional([])
更新 它看起来确实是一个元组,所以现在我有了这个:
func transformFromJSON(value: AnyObject?) -> RLMArray? {
let attachments = RLMArray(objectClassName: Attachment.className())
if let val:AnyObject = value {
Debug.log("-- new obj --")
if let arr = value as? Array<(AnyObject)> {
for file_name in arr {
Debug.log("fn: \(file_name)")
}
}
这允许我遍历不同的附件,但现在我仍然需要找出内部对象是什么。
更新 在获得带有“AnyObject”的元组数组后,该值似乎是一个 __NSCFDictionary,我可以使用 objectForKey 获取值
最佳答案
所以这似乎是如何将子数组转换为我的 RLMObject 附件的解决方案。我希望得到一些反馈,说明我在实现这个问题时有多么错误,以及什么是更合适的解决方案,因为我对此感到有点不好:
///Transformer 将api的附件数组转换为带附件的RLMArray ///NOTE: 回json暂时没有实现 类 ApiAttachmentsTransform:TransformType { typealias 对象 = RLMArray 类型别名 JSON = String
func transformFromJSON(value: AnyObject?) -> RLMArray? {
let attachments = RLMArray(objectClassName: Attachment.className())
if let val:AnyObject = value {
if let arr = value as? Array<(AnyObject)> {
for obj in arr {
let attachment = Attachment()
if let url = obj.valueForKey("url") as? String {
if let id = obj.valueForKey("id") as? Int {
attachment.url = url
attachment.id = id
attachments.addObject(attachment)
}
}
}
}
}
return attachments
}
func transformToJSON(value: RLMArray?) -> String? {
return nil
}
}
关于swift - 使用 ObjectMapper 从转换器获取子 RLMObjects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29970636/