【发布时间】:2019-02-23 20:38:16
【问题描述】:
我有一组属性/协议(背景是here,但我认为这是多余的)
类类型如下所示:
struct AdjustmentTypes {
internal class BaseType<T>: Hashable {
static func == (lhs: AdjustmentTypes.BaseType<T>, rhs: AdjustmentTypes.BaseType<T>) -> Bool {
return lhs.name == rhs.name
}
typealias A = T
var hashValue: Int { return name.hashValue }
let name: String
let defaultValue: T
let min: T
let max: T
var value: T
init(name: String, defaultValue: T, min: T, max: T) {
self.name = name
self.defaultValue = defaultValue
self.min = min
self.max = max
self.value = defaultValue
}
}
class FloatType: BaseType<CGFloat> { }
class IntType: BaseType<Int> { }
}
我正在使用类型擦除来删除类型,因此我可以将它们存储在 Set 中,并且我构建了一些辅助方法来简化我的 Set 工具:
class AdjustmentsSet {
private var adjustmentsSet: Set<AnyHashable> = []
func insert(_ adjustment: AnyHashable) {
adjustmentsSet.insert(adjustment)
}
func remove(_ adjustment: AnyHashable) {
adjustmentsSet.remove(adjustment)
}
func contains(_ adjustment: AnyHashable) -> Bool {
return adjustmentsSet.contains(adjustment)
}
var count: Int { return adjustmentsSet.count }
}
var adjustmentsSet = AdjustmentsSet()
我现在要做的是向我的Set 管理类添加一些帮助程序,以便能够检索具有正确类型的属性,例如如果我这样做:
let brightness = Brightness().make()
adjustments.get(brightness)
它应该返回nil,但如果我这样做:
adjustments.insert(brightness)
adjustments.get(brightness)
我现在应该取回值,作为它的正确类型,AdjustmentTypes.FloatType。
我正在考虑这样的Switch 声明:
class AdjustmentsSet {
// ...
func get(_ adjustment: AnyHashable) -> Any? {
guard let untyped = adjustmentsSet.first(where: { $0 == adjustment }) else { return nil }
switch adjustment {
case _ as AdjustmentTypes.FloatType: return untyped as! AdjustmentTypes.FloatType
case _ as AdjustmentTypes.IntType: return untyped as! AdjustmentTypes.IntType
default: return nil
}
}
}
然而,致命的缺陷当然是它返回Any,而不是预期的类型。
如何推断返回值的类型并返回正确的类型?
完整示例,只需将其放入游乐场:
// Generic conforming protocol to AnyHashable
protocol AnyAdjustmentProtocol {
func make() -> AnyHashable
}
protocol AdjustmentProtocol: AnyAdjustmentProtocol {
associatedtype A
func make() -> A
}
struct AdjustmentTypes {
internal class BaseType<T>: Hashable {
static func == (lhs: AdjustmentTypes.BaseType<T>, rhs: AdjustmentTypes.BaseType<T>) -> Bool {
return lhs.name == rhs.name
}
typealias A = T
var hashValue: Int { return name.hashValue }
let name: String
let defaultValue: T
let min: T
let max: T
var value: T
init(name: String, defaultValue: T, min: T, max: T) {
self.name = name
self.defaultValue = defaultValue
self.min = min
self.max = max
self.value = defaultValue
}
}
class FloatType: BaseType<CGFloat> { }
class IntType: BaseType<Int> { }
}
struct AnyAdjustmentType<A>: AdjustmentProtocol, Hashable {
static func == (lhs: AnyAdjustmentType<A>, rhs: AnyAdjustmentType<A>) -> Bool {
return lhs.hashValue == rhs.hashValue
}
private let _make: () -> AnyHashable
private let hashClosure:() -> Int
var hashValue: Int {
return hashClosure()
}
init<T: AdjustmentProtocol & Hashable>(_ adjustment: T) where T.A == A {
_make = adjustment.make
hashClosure = { return adjustment.hashValue }
}
func make() -> AnyHashable {
return _make()
}
}
struct Brightness: AdjustmentProtocol, Hashable {
func make() -> AnyHashable {
return AdjustmentTypes.FloatType(name: "Brightness", defaultValue: 0, min: 0, max: 1)
}
}
struct WhiteBalance: AdjustmentProtocol, Hashable {
func make() -> AnyHashable {
return AdjustmentTypes.IntType(name: "White Balance", defaultValue: 4000, min: 3000, max: 7000)
}
}
let brightness = Brightness().make()
let whiteBalance = WhiteBalance().make()
class AdjustmentsSet {
private var adjustmentsSet: Set<AnyHashable> = []
func insert(_ adjustment: AnyHashable) {
adjustmentsSet.insert(adjustment)
}
func remove(_ adjustment: AnyHashable) {
adjustmentsSet.remove(adjustment)
}
func contains(_ adjustment: AnyHashable) -> Bool {
return adjustmentsSet.contains(adjustment)
}
var count: Int { return adjustmentsSet.count }
}
var adjustmentsSet = AdjustmentsSet()
【问题讨论】:
标签: ios swift generics casting