ios - 非标称类型 'T' 不支持显式初始化 Codable
问题描述
我正在尝试创建一个响应库来包含 API 请求,但Non-nominal type 'T' does not support explicit initialization
在使用可编码时出现错误。这曾经与第三方库一起使用,但我正在将旧数据换成新数据。
class ResponseBase: Codable {
var status: String?
var message: String?
var pagination: Pagination?
var isSucessful: Bool {
return status == "success"
}
struct ErrorMessage {
static let passwordInvalid = " Current password is invalid."
static let loginErrorIncorrectInfo = " Incorrect username/password."
static let loginErrorAccountNotExist = " Invalid request"
}
}
class Response<T: Codable>: ResponseBase {
var data: T?
public func setGenericValue(_ value: AnyObject!, forUndefinedKey key: String) {
switch key {
case "data":
data = value as? T
default:
print("---> setGenericValue '\(value)' forUndefinedKey '\(key)' should be handled.")
}
}
public func getGenericType() -> Codable {
return T()
}
}
解决方案
public func getGenericType() -> T.Type {
return T.self
}
推荐阅读
- java - 如何从 Servlet 过滤器呈现 Thymeleaf 模板?
- http - 是否有可能在 CORS 之前进行 CSRF 攻击?
- raku - List repetition (xx) without evaluation?
- python - 如何使用 Python 中的 smtplib 发送文本或 word 文件?
- opencv - Can I move my camera after intrinsic calibration?
- c# - 如何使用 lambda 表达式从 LINQ 中的左连接中提取 max(property)
- python - python-docx 检测枚举列表
- liferay - liferay Dxp:当单击超链接时,如何从一个页面中的 portel 重定向到包含不同 portlet 的另一个页面
- node.js - 将文件从一个节点 js 服务器发送到另一个
- django - Django 表单未保存,终端显示数据并添加了保存方法