首页 > 解决方案 > 使用泛型创建结构数组

问题描述

我正在尝试创建一个使用泛型设置属性类型的结构。

这工作正常,如下所示,但是我希望能够遍历这些结构的集合并检查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]

我怎样才能做到这一点?

标签: swiftgenericscodable

解决方案


的泛型参数CacheWrapper必须是符合 的具体类型Codable。协议不能符合自己。

一个解决方案是创建一个需要实现的协议expiryid如果有必要)

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) }

推荐阅读