1

I'm using both ObjectMapper (https://github.com/Hearst-DD/ObjectMapper) and Realm in my project. My Objects are all RLMObjects;

I have for example a Blog Object which contains attachments:

dynamic var attachments = RLMArray(objectClassName: Attachment.className())

I have a custom transformer which starts like this:

func transformFromJSON(value: AnyObject?) -> RLMArray? {
            let attachments = RLMArray(objectClassName: Attachment.className())
            if let str = value as? String {

I can't seem to understand how I could convert them to a RLMArray, I always get nil in my transformer.

"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"
    }
  ],

I find it hard to wrap my head around the code, also I dont' find much help in the debugger of xcode.

TL;DR My transformer recieves a nil, or I'm expecting the wrong type how to convert the value to a RLMArray.

Update: I hope it's getting a bit clear what I'm struggling with. Anyhow I changed my transformer to contain this for debugging sake:

    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
    }

Even more confusing, the first Debug.log (which just makes a NSLog). outputs the following:

 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";
    }
)

Which is nor json, nor an array nor a dictionary as far as I can see cause those log lines return:

the val is this nil or, for an array the val is this Optional([])

update It did seem to be a tuple so now I have this:

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

Which allows me to iterate over the different attachments, now I still have to find out what the inner object is though.

update After getting a tuple array with "AnyObject" it seems the value was a __NSCFDictionary, which I could fetch values from using objectForKey

Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75
  • What exactly is your question here? You say you find it hard to debug, but you haven't mentioned any bugs. – ravron Apr 30 '15 at 15:00
  • I also can't understand what you're asking. Can you be more specific about *why* you're having difficulty debugging? – jpsim Apr 30 '15 at 23:24
  • I guess it's specific to the ObjectMapper package, the transformer is extending the Transformer from objectmapper. I expect a string which I could convert to a JSON array and create the RLMArray from, however I keep getting a nil value in my transformer. Does this make more sense? – Mathijs Segers May 01 '15 at 06:41
  • I just realized this must be a Tuple, I was unaware this existed and I better find out how. – Mathijs Segers May 01 '15 at 07:45

1 Answers1

1

So this seems to be the solution how to convert an array of children to my RLMObject Attachment. I'd love some feedback on how wrong I am implementing this and what would be a more proper solution since I feel kinda bad for this:

///Transformer transforming the api's attachment array into a RLMArray with attachments ///NOTE: Back to json is not implemented at this time 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
    }
}
Mathijs Segers
  • 6,168
  • 9
  • 51
  • 75