我为API响应创建了Decodable基本结构。

struct ResponseBaseModel<T: Decodable>: Decodable {
    let status: Bool
    let message: String
    var result: T?

    private enum CodingKeys: String, CodingKey {
        case result, message, success,status,statusCode
    }

     init(from decoder: Decoder) throws {
           let values = try decoder.container(keyedBy: CodingKeys.self)
            if let result = try? values.decode(T.self, forKey: .result) {
                self.result = result
            }
        status = try  values.decode(Bool.self, forKey: .status)
        message = try  values.decode(String.self, forKey: .message)
       }
}


//这是API响应

{
    "status": true,
    "statusCode": 200,
    "message": "Theater list successfully",
    "result": [
        {
            "id": 1,
            "name": "Galaxy",
            "picture": "https://ctdemo.workpc.online/kshatrainfotech/abol-app/public/storage/images/theaters/default.png",
            "is_notify": false
        }
    ]
}


这是我如何使用它来调用API

 apimanager.fetch { [weak self] (response: Result<ResponseBaseModel<[Theater]>, ApiError>) in
        self?.handelResponse(response: response) { response in
            switch response {
            case .success(let theaterList):
                self?.theaterViewModels = theaterList.map{ TheaterViewModel(theaterModel: $0)}
                self?.responseHandler(.success(self!.theaterViewModels))
            case .failure(let apiError):
                self?.responseHandler(.failure(apiError))
            }
        }
    }


但某些API没有这样的结果

{
    “状态”:是,
    “ statusCode”:200,
    “消息”:“ api.DATA_UPDATED_SUCCESS”
}

如何使用ResponseBaseModel模型处理上述响应。因为在使用ResponseBaseModel结构时必须传递任何类型。

ResponseBaseModel<?>


我已经尝试过ResponseBaseModel<nil>,但是没有用。

最佳答案

我尝试简单地回答您的问题,以说明为什么您想要的东西无法通过这种方式实现。考虑以下结构:

struct TestStruct<T> {
    let code:Int
    var value:T?

    init(code:Int) {
        self.code = code
    }
}


通常,您可以通过以下方式使用它:

var s = TestStruct<String>(code:42)
s.value = "abc"

print (s) // TestStruct<String>(code: 42, value: Optional("abc"))


现在您说:“哦,我只需要一个TestStruct来存储一些代码,根本没有任何价值”,然后尝试以下操作:

var codeOnly = TestStruct<nil>(code:42)
print (codeOnly)


编译器抱怨,因为它需要value属性的专用类型。
为什么不起作用?因为考虑以下语句:

let v = codeOnly.value
// or
codeOnly.value = "String"


在这里,编译器需要知道属性codeOnly.value是什么类型。
由于现在不能,因此将不允许使用无类型的TestStruct

10-07 19:49
查看更多