簡體   English   中英

無法轉換'_?'類型的值 輸入'[AFTParty]?' 在脅迫中 - 斯威夫特

[英]Cannot convert value of type '_?' to type '[AFTParty]?' in coercion - Swift

我正在讀取一個返回對象數組的JSON。 我想將JSON響應綁定到我的對象AFTParty,並且我遇到了這個編譯錯誤:

無法轉換'_?'類型的值 輸入'[AFTParty]?'

AFTParty是一個符合Decodable協議的類,我正在使用Himotoki來完成這項工作。

我的JSON:

[
{
id: 1,
partyName: "x",
partyAuthor: [
{
id: 1,
username: "mtfons",
name: "marco",
lastname: "fonseca",
phoneNumber: "91859158",
email: "mt.fonseca23@gmail.com"
}
],
partyDescription: null,
partyStyle: null,
partyPermission: "PUBLICA",
partyLocation: {
id: 1,
latitude: "1248174",
longitude: "1428149"
},
prices: [ ],
partyDetailToJoin: "x",
partyDateBegin: null,
partyDateEnd: null,
partyImagesPath: [ ],
partyParticipants: [
{
id: 1,
username: "x",
name: "x",
lastname: "x",
phoneNumber: "x",
email: "x@gmail.com"
},
{
id: 2,
username: "x",
name: "ajfk",
lastname: "jfa",
phoneNumber: "x",
email: "teste@teste.com"
}
],
time: null,
partyType: null
}
]

給出錯誤的代碼:

      let partyDeserializer = AFTPartyDeserializerImplementation(network: network)
            describe("Response") {
                it("returns party if the network works correctly.") {
                    var response = [AFTParty]()
                    partyDeserializer.getParty().on(next: { response = $0 }).start()

                }
     }


       public func getParty() -> SignalProducer<AFTParty, AFTNetworkError> {
            let url = AFTRouter.Parties.URL
            return network.requestJSON(url, parameters: nil)
                .attemptMap { json in
                    if let response = (try? decodeValue(json)) as [AFTParty]? {
                        return Result(value: response)
                    }
                    else {
                        return Result(error: .IncorrectDataReturned)
                    }
            }
        }

謝謝你的幫助,我很抱歉我的英語不好。

你的意思是

public func getParty() -> SignalProducer<[AFTParty], AFTNetworkError> {

如果decodeValue(json)返回AnyObject ,那么你可能想要使用as? ,不as

暫無
暫無

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

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