- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我有一些结构符合的基本协议(protocol)(模型)。它们也符合 Hashable
protocol Model {}
struct Contact: Model, Hashable {
var hashValue: Int { return ... }
static func ==(lhs: Contact, rhs: Contact) -> Bool { return ... }
}
struct Address: Model, Hashable {
var hashValue: Int { return ... }
static func ==(lhs: Address, rhs: Address) -> Bool { return ... }
}
我有一个函数,它接受一个符合模型 ([Model]) 的对象数组。如何将 [Model] 传递给需要 Hashables 的函数而不使 Model Hashable?
func complete(with models: [Model]) {
doSomethingWithHashable(models) //can't do this
}
func doSomethingWithHashable <T:Hashable>(_ objects: [T]) {
//
}
我正在努力避免这种情况
protocol Model: Hashable {}
func complete<T:Model>(with models: [T]) {
runComparison(models)
}
因为当我执行此操作时得到“模型不能用作通用约束...”
protocol SomethingElse {
var data: [Model] { get }
}
最佳答案
你的代码的问题是你在谈论 Model
,它 promise 什么关于Hashable
一致性。正如您所指出的,将此告诉编译器的问题(即从 Model
派生 Hashable
)是您随后失去了根据符合 Model
的异构类型进行对话的能力。 .
如果你根本不关心Model
首先一致性,你可以只使用标准库的 AnyHashable
完全任意的类型删除包装器Hashable
符合条件的实例。
但是,假设您确实关心 Model
一致性,您必须构建自己的 type-erased wrapper对于同时符合 Model
的实例和 Hashable
.在 my answer here ,我演示了如何为 Equatable
构建类型橡皮擦符合类型。那里的逻辑可以很容易地扩展为 Hashable
– 我们只需要存储一个额外的函数来返回 hashValue
实例的。
例如:
struct AnyHashableModel : Model, Hashable {
static func ==(lhs: AnyHashableModel, rhs: AnyHashableModel) -> Bool {
// forward to both lhs's and rhs's _isEqual in order to determine equality.
// the reason that both must be called is to preserve symmetry for when a
// superclass is being compared with a subclass.
// if you know you're always working with value types, you can omit one of them.
return lhs._isEqual(rhs) || rhs._isEqual(lhs)
}
private let base: Model
private let _isEqual: (_ to: AnyHashableModel) -> Bool
private let _hashValue: () -> Int
init<T : Model>(_ base: T) where T : Hashable {
self.base = base
_isEqual = {
// attempt to cast the passed instance to the concrete type that
// AnyHashableModel was initialised with, returning the result of that
// type's == implementation, or false otherwise.
if let other = $0.base as? T {
return base == other
} else {
return false
}
}
// simply assign a closure that captures base and returns its hashValue
_hashValue = { base.hashValue }
}
var hashValue: Int { return _hashValue() }
}
然后你可以像这样使用它:
func complete(with models: [AnyHashableModel]) {
doSomethingWithHashable(models)
}
func doSomethingWithHashable<T : Hashable>(_ objects: [T]) {
//
}
let models = [AnyHashableModel(Contact()), AnyHashableModel(Address())]
complete(with: models)
这里我假设您还想将它用作 Model
的包装器的要求(假设有一些)。或者,您可以公开 base
属性并删除 Model
符合 AnyHashableModel
本身,让调用者访问 base
对于基础 Model
符合实例:
struct AnyHashableModel : Hashable {
// ...
let base: Model
// ...
}
但是您会注意到,上述类型删除包装器仅适用于同时为 Hashable
的类型。和一个 Model
.如果我们想讨论符合实例为 Hashable
的其他协议(protocol)怎么办? ?
一个更通用的解决方案,正如我演示的那样in this Q&A , 而是接受同时为 Hashable
的类型并符合其他一些协议(protocol)——其类型由通用占位符表示。
因为目前在 Swift 中没有办法表达一个必须符合另一个通用占位符给定协议(protocol)的通用占位符;此关系必须由调用者使用 transform
定义闭包来执行必要的向上转换。然而,由于 Swift 3.1 在扩展中接受了具体的相同类型要求,我们可以定义一个方便的初始化程序来删除 Model
的样板文件。 (对于其他协议(protocol)类型可以重复此操作)。
例如:
/// Type-erased wrapper for a type that conforms to Hashable,
/// but inherits from/conforms to a type T that doesn't necessarily require
/// Hashable conformance. In almost all cases, T should be a protocol type.
struct AnySpecificHashable<T> : Hashable {
static func ==(lhs: AnySpecificHashable, rhs: AnySpecificHashable) -> Bool {
return lhs._isEqual(rhs) || rhs._isEqual(lhs)
}
let base: T
private let _isEqual: (_ to: AnySpecificHashable) -> Bool
private let _hashValue: () -> Int
init<U : Hashable>(_ base: U, upcast: (U) -> T) {
self.base = upcast(base)
_isEqual = {
if let other = $0.base as? U {
return base == other
} else {
return false
}
}
_hashValue = { base.hashValue }
}
var hashValue: Int { return _hashValue() }
}
// extension for convenience initialiser for when T is Model.
extension AnySpecificHashable where T == Model {
init<U : Model>(_ base: U) where U : Hashable {
self.init(base, upcast: { $0 })
}
}
您现在希望将实例包装在 AnySpecificHashable<Model>
中:
func complete(with models: [AnySpecificHashable<Model>]) {
doSomethingWithHashable(models)
}
func doSomethingWithHashable<T : Hashable>(_ objects: [T]) {
//
}
let models: [AnySpecificHashable<Model>] = [
AnySpecificHashable(Contact()),
AnySpecificHashable(Address())
]
complete(with: models)
关于swift - 检查 Hashable 一致性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43263352/
GhostScript PDF/A 生成好像有错误 当您通过 GhostScript 生成 PDF/A 文档时,当您单击 Adobe Reader 时,会出现一个一致性选项卡,其中显示: “一致性
我有一个需要测试的 XPath 引擎实现。 是否有一组标准的一致性测试可以用来验证是否符合 XPath 规范(与 XSLT 相关)。 什么将是完美的 XML 文档 XPath 表达式和预期的结果。 最
帮助我了解在这种情况下我可以期望与 MongoDB 的一致性级别。 我们正在运行一个副本集,其中 Mongoid 中的 consistency 标志设置为 strong,这意味着只读到 master。
假设我有一个采用一个参数的方法。 此参数应满足以下要求: 'of type':方法需要知道参数属于特定类(或子类)。 'implements interface':方法需要知道参数实现了特定的接口(i
当协议(protocol)将属性声明为可选而具体类型将其声明为非可选时,如何使具体类型符合协议(protocol)? 这是问题所在: protocol Track { var trackNum
我正在考虑使用浏览器的 navigator.mimeTypes 数组作为第三级用户/浏览器标识符。例如,当我在 Chrome 上运行时... console.log(navigator.mimeTyp
我有以下协议(protocol): protocol ProtoAInput { func funcA() } protocol ProtoA { var input: ProtoAI
如果选择“最终”一致性,则发生写入的区域内的一致性是什么? 如果我只需要区域强一致性,应该选择哪个选项? 最佳答案 如果您需要在主要区域内进行强读取,则应该选择强一致性或有界过时一致性。 关于azur
您好,我是一名初学者,目前正在尝试学习 java 编程。课本上的问题: 编写一个程序来帮助人们决定是否购买混合动力汽车。你的程序的输入应该是:•新车的成本•预计每年行驶里程•预计汽油价格 •每加仑英里
我正在尝试制作一个可以在 UILabel 上使用的 Swift 协议(protocol), UITextField , 和 UITextView包含他们的text , attributedText ,
我有一个类扩展: extension UICollectionViewCell { class func registerFromNibInCollectionView(collectionV
为了在 Swift 中模拟对象进行测试,我通常遵循这样的模式:编写一个协议(protocol)来描述我想要的对象的行为,然后使用 Cuckoo 为其生成模拟以进行测试。 通常,这些协议(protoco
假设我有两个非通用协议(protocol)(1) protocol StringValue { var asString: String {get} } protocol StringProv
我有一组协议(protocol)可以在 UITableView 中显示一个元素: protocol TableRepresentableRow { var title: String { get
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
用“class”标记 CacheManager 解决了我的问题。 案例:一个简单的缓存器,mutating get 不是我想要的,那么对于引用类型或类类型应该怎么做? protocol Cacher
我想要一个符合协议(protocol)的变量,但是 swift 编译器告诉我协议(protocol)没有确认。 protocol A {} protocol B { var a : A { g
如果我有一个类 Christmas 和一个协议(protocol) Merry,要使 Christmas 符合 Merry,很多人会这样做: class Christmas { ... } e
@objc public protocol P1 { func p1foo() } @objc public protocol P2 { func p2foo() } class A: NSO
我有一些结构符合的基本协议(protocol)(模型)。它们也符合 Hashable protocol Model {} struct Contact: Model, Hashable { v
我是一名优秀的程序员,十分优秀!