AlamofireJson / EVReflection - NSArray元素无法匹配Swift数组元素类型

时间:2016-07-01 06:17:26

标签: ios swift alamofire

还将此问题视为项目GitHub中的错误:https://github.com/evermeer/AlamofireJsonToObjects/issues/24

使用框架: Alamofire(用于REST) AlamofireJsonToObjects(反序列化JSON) (依赖于EVReflection)

在尝试获取对象内部的对象数组以进行反序列化时,我遇到了问题。输出结果为fatal error: NSArray element failed to match the Swift Array Element type,然后调试器在EVReflection.swift mi = Mirror(reflecting: theValue)的第560行中断,错误为:Thread 1: EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0)

当嵌套对象不在数组中时,它能够反序列化嵌套对象,所以不确定这里可能存在什么问题。我会在下面粘贴一些代码,以便您可以看到我的流程。

Alamofire请求,当我要求它打印debugDescription

时会发生错误
Alamofire.request(.GET, "http://server.com/api/get/all", headers: getAuthenticationHeaders()).
validate().
responseArray { (response: Result<[MyPrimaryObject], NSError>) in
       if(response.isSuccess){
           print(response.debugDescription)
           success(response.value)
       }
       if(response.isFailure){
           let error : NSError = response.error!
           NSLog("API failure: \(error.debugDescription)")
           failure(error)
       }
   }

型号:

public class MyPrimaryObject : EVObject{

    public var myPrimaryObjectId : NSUUID?
    public var name : String = ""
    public var myObjectDescription: String?

    public var numberOfOccurrences : Int = 0
    public var positiveResponsePercentage : Float = 0

    public var secondaryObjects : [MySecondaryObject]?

    override public func propertyMapping() -> [(String?, String?)] {
        return [("myObjectDescription","Description")]

    }
}

public class MySecondaryObject : EVObject {
    public var mySecondaryObjectId : Int = 0
    public var dateRecorded : NSDate?
    public var rating : Int = 0
    public var userRemarks : String?
}

还应该注意,当MyPrimaryObject在API响应中为空时,secondaryObjects会被完美地解析。

我显然简化了,我很高兴私下与你分享实际的代码。任何帮助将不胜感激!非常感谢!

1 个答案:

答案 0 :(得分:1)

正如我所报道的那样 https://github.com/evermeer/AlamofireJsonToObjects/issues/24

问题是当你有一个子对象数组时,pascal大小写转换不能用于获取正确的类型。因此,字典被放入数组中,你会得到你得到的错误。

此修订已发布在EVReflection 2.38.3