使用ObjectMapper从变换器获取子RLMObjects

时间:2015-04-30 14:37:41

标签: swift realm object-object-mapping

我在我的项目中使用了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,我的变压器总是没有。

"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我的变压器收到一个零,或者我期望错误的类型如何将值转换为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获取值

1 个答案:

答案 0 :(得分:1)

所以这似乎是如何将子数组转换为我的RLMObject附件的解决方案。我喜欢一些关于我实施这个错误的反馈,以及什么是更合适的解决方案,因为我觉得有点不好:

/// Transformer将api的附件数组转换为带附件的RLMArray     ///注意:目前尚未实现返回json     class ApiAttachmentsTransform:TransformType {         typealias Object = RLMArray         typealias 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
    }
}