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