2014-06-06 27 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協議。或者在比較時使用===而不是==,如果您確定沒有不同的實例「相等」。