问题
I am using RestKit and realm.io together. I have an array of values (the array is url strings of pictures) being returned in JSON that should become a RLMArray
of RLMObject
s. I believe the mappings are set up correctly because it makes an attempt to convert the results to an RLMArray
. But I get the following error from RestKit:
restkit.object_mapping:RKMappingOperation.m:449 Failed transformation of value at keyPath 'picList' to representation of type 'RLMArray': Error Domain=org.restkit.RKValueTransformers.ErrorDomain Code=3002 "Failed transformation of value '(
"Picture {\n\turl = ;\n\tremote_url = http://placekitten.com/g/500/500;\n}",
"Picture {\n\turl = ;\n\tremote_url = http://placekitten.com/g/400/400;\n}",
"Picture {\n\turl = ;\n\tremote_url = http://placekitten.com/g/300/300;\n}"
)' to RLMArray: none of the 2 value transformers consulted were successful."
So I created a value transformer to perform the transformation manually. Here is my code:
func setupValueTransformerForPicList(){
println("called value transformer function")
var picListValueTransformer = RKBlockValueTransformer(validationBlock: { (inputClass:AnyClass!,outputClass:AnyClass!) -> Bool in
if (inputClass.className() == "Array" || inputClass.className() == "__NSArrayM") && outputClass.className() == "RLMArray" {
return true
}
return false
}) { (inputValue:AnyObject!, var outputValue, outputClass, error) -> Bool in
println("called value transformer")
if let thisArray:NSArray = inputValue as? NSArray{
var picRlmArray:RLMArray = RLMArray(objectClassName: Picture.className())
for item in thisArray {
if let thisPicture:Picture = item as? Picture{
picRlmArray.addObject(thisPicture)
} else {
return false
}
}
outputValue = picRlmArray // this is the line that throws the error
return true
}
return false
}
RKValueTransformer.defaultValueTransformer().addValueTransformer(picListValueTransformer)
}
However, I get the error message:
'RLMArray' is not convertible to 'AutoreleasingUnsafeMutablePointer<AnyObject?>'
I've tried downcasting
outputValue = picRlmArray as AutoreleasingUnsafeMutablePointer<AnyObject?>
with the same result. I've tried doing whatever this is (if it's even a thing)
outputValue = picRlmArray as AutoreleasingUnsafeMutablePointer<RLMArray?>
and get the error
'RLMArray' is not identical to 'AnyObject'
I'm a little out of my depth here, but feel like this should definitely be achievable. Any help would be greatly appreciated. Thank you!
EDIT:
I'm also unable to return a swift array from this function. It gives the same error. I think this might be a more general question about how to return types that aren't AnyObject
s.
回答1:
I'm the current maintainer of RestKit, and I'm sad to say that I haven't had much of a chance to play around with using RestKit and Realm together, especially not with Swift.
I'll take a look at what might be causing this error.
EDIT:
It seems to me that doing outputValue.memory = picRlmArray
instead of outputValue = picRlmArray
would do the trick!
来源:https://stackoverflow.com/questions/27302925/converting-nsarray-to-rlmarray-with-rkvaluetransformer-fails-converting-outputva