2014-06-06 20 views
2

我想获取当前主题的索引。 我的主题列表声明如下方式Swift中的数组<Prototocol>中的对象索引

var themes:Array<ThemeProtocol> = [] 

我使用 let currentIndex = find(self.themes, self.currentTheme)尝试,但它不工作。 enter image description here

我也使用

func currentThemeIndex()->Int? { 
     let indecies = enumerate(self.themes) 
     for (index, item) in indecies { 
      if self.currentTheme == item { 
       return index 
      } 
     } 
     return nil 
    } 

enter image description here 任何想法试了一下我做错了吗?

回答

3

==要求有两个对象上的等价运算符(认为isEqual:从目标C)

===是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参考指南免费通过h iTunes书店。

注意,这些功能与模块范围内定义(即,外部的任何类和/或结构声明)

1

确保ThemeProtocol符合Equatable协议。或者在比较时使用===而不是==,如果您确定没有不同的实例“相等”。

相关问题