我一起使用RestKit和realm.io。我有一个值数组(数组是url图片的字符串)以JSON格式返回,应该成为RLMArray
RLMObject
的{{1}}。我相信映射设置正确,因为它尝试将结果转换为RLMArray
。但我从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."
所以我创建了一个值变换器来手动执行转换。这是我的代码:
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)
}
但是,我收到错误消息:
'RLMArray' is not convertible to 'AutoreleasingUnsafeMutablePointer<AnyObject?>'
我尝试过向下转发
outputValue = picRlmArray as AutoreleasingUnsafeMutablePointer<AnyObject?>
具有相同的结果。我已经尝试过这样做(如果它甚至是一件事)
outputValue = picRlmArray as AutoreleasingUnsafeMutablePointer<RLMArray?>
并获得错误
'RLMArray' is not identical to 'AnyObject'
我在这里有点不太深入,但我觉得这绝对是可以实现的。任何帮助将不胜感激。谢谢!
修改
我也无法从此函数返回swift数组。它给出了同样的错误。我认为这可能是一个更普遍的问题,关于如何返回不是AnyObject
s。
答案 0 :(得分:4)
I&#39; M RestKit的当前维护者,和I&#39; M悲伤地说,我的天堂&#39;吨有多少机会使用RestKit与境界在一起,特别是不与夫特玩弄的。
我将查看可能导致此错误的原因。
编辑:
在我看来,做outputValue.memory = picRlmArray
而不是outputValue = picRlmArray
就行了!