无法转换类型' _?'的值键入' [AFTParty]?'在胁迫中 - 斯威夫特

时间:2016-04-19 10:45:35

标签: arrays swift alamofire reactive-cocoa

我正在阅读一个返回对象数组的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)
                    }
            }
        }

感谢您的帮助,我很抱歉我的英语不好。

2 个答案:

答案 0 :(得分:0)

你的意思是

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

答案 1 :(得分:0)

如果decodeValue(json)返回AnyObject,那么您可能希望使用as?而不是as投射。