簡體   English   中英

AlamofireJson / EVReflection-NSArray元素無法與以下中的Swift Array Element類型匹配

[英]AlamofireJson/EVReflection - NSArray element failed to match the Swift Array Element type in

還問這個問題作為項目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?
}

還應注意,當API響應中的secondaryObjects為null時,將完美解析MyPrimaryObject

我顯然已經簡化了,很高興與您私下分享實際的代碼。 任何幫助將不勝感激! 非常感謝!

正如我在https://github.com/evermeer/AlamofireJsonToObjects/issues/24中報道的那樣

問題是,當您有一個子對象數組時,pascal大小寫轉換無法獲得正確的類型。 因此,將字典放入數組中,您將得到錯誤。

該修復程序已在EVReflection 2.38.3中發布

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM