swift - 使用泛型创建结构数组
问题描述
我正在尝试创建一个使用泛型设置属性类型的结构。
这工作正常,如下所示,但是我希望能够遍历这些结构的集合并检查expiry
道具,以便在它过期时采取行动。
enum ContainerExpiryType {
case seconds(_ seconds: Int), minutes(_ minutes: Int), hours(_ hours: Int), days(_ days: Int), date(_ date: Date)
private var calender: Calendar {
return Calendar.current
}
var date: Date? {
switch self {
case .seconds(let seconds):
return calender.date(byAdding: .second, value: seconds, to: Date())
case .minutes(let minutes):
return calender.date(byAdding: .minute, value: minutes, to: Date())
case .hours(let hours):
return calender.date(byAdding: .hour, value: hours, to: Date())
case .days(let days):
return calender.date(byAdding: .day, value: days, to: Date())
case .date(let date):
return date
}
}
}
struct CacheWrapper<T: Codable>: Codable {
var id: UUID
var expiry: Date
var object: T
init(object: T, expiry: ContainerExpiryType) {
self.id = UUID()
self.expiry = expiry.date ?? Date()
self.object = object
}
}
let a = CacheWrapper<String>(object: "foo", expiry: .days(1))
let b = CacheWrapper<String>(object: "bar", expiry: .days(2))
let c = CacheWrapper<Int>(object: 5, expiry: .days(-100))
let foo: [CacheWrapper<Codable>] = [a, b, c]
foo.forEach { print($0.expiry) }
但是这样做会引发错误
错误:CacheExp.playground:45:11:错误:协议类型'Codable'(又名'Decodable & Encodable')不能符合'Decodable',因为只有具体类型才能符合协议 let foo: [CacheWrapper] = [a,b ,C]
我怎样才能做到这一点?
解决方案
的泛型参数CacheWrapper
必须是符合 的具体类型Codable
。协议不能符合自己。
一个解决方案是创建一个需要实现的协议expiry
(id
如果有必要)
protocol Wrappable {
var id: UUID { get }
var expiry: Date { get }
}
采纳Wrappable
struct CacheWrapper<T: Codable>: Codable, Wrappable { ...
并注释
let foo: [Wrappable] = [a, b, c]
然后就可以打印了
foo.forEach { print($0.expiry) }
推荐阅读
- c++ - 有没有一种通用的方法来查找任何以空字符结尾的字符串的长度?
- java - 安装eclipse时找不到Java虚拟机
- excel - Excel VB 用户表单有条不紊地完成缺失的数据
- flutter - 在 setState 中使用 Navigator
- java - 在 Websphere 上运行 Project 时找不到要添加到服务器的有效父模块
- python-requests - 使用字典的预算程序
- java - 具有2个数组列表的非递归合并排序
- vba - 获取 Dymo 标签的对象名称
- javascript - Uncaught (in promise) TypeError: cannot read property graphics of undefined
- ios - (目标 C)TABLEVIEW 单元格标识符重要吗?