如何使用可选的泛型类型创建动态结构

时间:2020-01-29 12:13:47

标签: ios swift generics

我为API响应创建了可分解的基本结构。

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没有

之类的结果

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

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

ResponseBaseModel<?>

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

1 个答案:

答案 0 :(得分:0)

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

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

相关问题