我想获取当前主题的索引。 我的主题列表按以下方式声明
var themes:Array<ThemeProtocol> = []
我尝试使用
let currentIndex = find(self.themes, self.currentTheme)
但它不起作用。
我也尝试过使用
func currentThemeIndex()->Int? {
let indecies = enumerate(self.themes)
for (index, item) in indecies {
if self.currentTheme == item {
return index
}
}
return nil
}
知道我做错了什么吗?
最佳答案
==
要求两个对象有一个等价运算符(想想 Objective C 中的 isEqual:
)
===
相当于 Objective-C 运算符 ==
要使对象等价操作起作用,您需要定义一个等价运算符:
@infix func == (left:Vector2D, right: Vector2D) -> Bool {
return left.x == right.x && left.y == right.y
}
@infix func != (left:Vector2D, right:Vector2D) -> Bool {
return !(left == right)
}
这一切都直接来自 Apple 引用指南,可通过 iTunes 书店免费获取。
请注意,这些函数是在模块范围内定义的(即在任何类和/或结构声明之外)
关于cocoa - Swift 中 Array<Prototocol> 中对象的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24085254/