Arrays 符合不同类/结构的索引
我有两个不同的类/结构,我希望能够使用:Arrays 符合不同类/结构的索引,arrays,swift,Arrays,Swift,我有两个不同的类/结构,我希望能够使用: let index = [T].indexOf(U) 游乐场的一些示例数据: struct Foo: Hashable { let name: String var hashValue: Int { return name.hashValue } } struct Bar: Hashable { let name: String var hashValue: Int { return name.hashValue }
let index = [T].indexOf(U)
游乐场的一些示例数据:
struct Foo: Hashable {
let name: String
var hashValue: Int { return name.hashValue }
}
struct Bar: Hashable {
let name: String
var hashValue: Int { return name.hashValue }
}
func ==(lhs: Foo, rhs: Foo) -> Bool { return lhs.name == rhs.name }
func ==(lhs: Bar, rhs: Bar) -> Bool { return lhs.name == rhs.name }
func ==(lhs: Bar, rhs: Foo) -> Bool { return lhs.name == rhs.name }
尝试:
let test1 = Foo(name: "John")
let test2 = Foo(name: "Amy")
let test3 = Bar(name: "Mary")
let test4 = Bar(name: "John")
let arrays = [test1, test2]
let result = arrays.indexOf(test3) // cannot convert value of type 'Bar' to expected argument type 'Foo'
let arrays: [Names] = [test1, test2]
let result = arrays.indexOf(test2) // cannot convert value of type 'Foo' to expected argument type '@noescape (Names) throws -> Bool'
我应该用协议来代替吗
使用协议:
protocol Names {
var name: String { get set }
}
extension Equatable where Self: Names { }
struct Foo: Names { var name: String }
struct Bar: Names { var name: String }
func ==<T: Names>(lhs: T, rhs: T) -> Bool {
return lhs.name == rhs.name
}
以及:
问题在于
equalable
的操作员过载定义为:
func ==(_ lhs: Self, _ rhs: Self) -> Bool
因此,它包含在它所操作的对象的类型中-在您的例子中是Foo
或Bar
,但不是两者的混合。equalable
永远不会使用以下声明:
func ==(lhs: Bar, rhs: Foo) -> Bool { return lhs.name == rhs.name }
你能把你的结构改变成一个共同祖先的子类吗?大致如下:
class FooBar: Hashable {
var name: String!
var hashValue: Int { return name.hashValue }
init() { }
}
class Foo: FooBar {
}
class Bar: FooBar {
}
func ==(lhs: FooBar, rhs: FooBar) -> Bool { return lhs.name == rhs.name }
var test1 = Foo(); test1.name = "John"
var test2 = Foo(); test2.name = "Amy"
var test3 = Bar(); test3.name = "Mary"
var test4 = Bar(); test4.name = "John"
let arrays: [FooBar] = [test1, test2]
let result = arrays.indexOf(test4)
print(result)
问题在于
equalable
的操作员过载定义为:
func ==(_ lhs: Self, _ rhs: Self) -> Bool
因此,它包含在它所操作的对象的类型中-在您的例子中是Foo
或Bar
,但不是两者的混合。equalable
永远不会使用以下声明:
func ==(lhs: Bar, rhs: Foo) -> Bool { return lhs.name == rhs.name }
你能把你的结构改变成一个共同祖先的子类吗?大致如下:
class FooBar: Hashable {
var name: String!
var hashValue: Int { return name.hashValue }
init() { }
}
class Foo: FooBar {
}
class Bar: FooBar {
}
func ==(lhs: FooBar, rhs: FooBar) -> Bool { return lhs.name == rhs.name }
var test1 = Foo(); test1.name = "John"
var test2 = Foo(); test2.name = "Amy"
var test3 = Bar(); test3.name = "Mary"
var test4 = Bar(); test4.name = "John"
let arrays: [FooBar] = [test1, test2]
let result = arrays.indexOf(test4)
print(result)
我知道这可能不是您想要的,但是您可以始终执行
arrays.indexOf({$0==test3})
是的,我知道这是可能的。谢谢你的提示。我知道这可能不是你想要的,但是你可以一直做arrays.indexOf({$0==test3})
是的,我知道这是可能的。谢谢你的提示。这些信息对我非常有用,但不幸的是类已经从NSManagedObject
继承,所以我不能使用这个方法。这些信息对我非常有用,但不幸的是类已经从NSManagedObject
继承,所以我不能使用这个方法。