在Swift 3中replace数组的indexOf(_ :)方法

在我的项目(用Swift 3编写)中,我想使用indexOf(_:)方法(存在于Swift 2.2中)从数组中检索一个元素的索引,但是我找不到任何replace。

Swift 3中的这个方法有什么好的替代方法吗?

更新

我忘了提及,我想在自定义对象中search。 在代码完成时,我没有任何提示input“indexof”。 但是当我试图得到类似Int代码完成工作的构build索引,我可以使用index(of:)方法。

indexOf(_:)已被重命名为符合Equatabletypes的index(of:) 。 您可以将您的任何types符合Equatable ,这不仅适用于内置types:

 struct Point: Equatable { var x, y: Int } func == (left: Point, right: Point) -> Bool { return left.x == right.x && left.y == right.y } let points = [Point(x: 3, y: 5), Point(x: 7, y: 2), Point(x: 10, y: -4)] points.index(of: Point(x: 7, y: 2)) // 1 

需要闭包的indexOf(_:)已经被重命名为index(where:)

 [1, 3, 5, 4, 2].index(where: { $0 > 3 }) // 2 // or with a training closure: [1, 3, 5, 4, 2].index { $0 > 3 } // 2 

在Swift 3 XCode 8中,我没有为我工作,直到我给我的课程延期 。

例如:

 class MyClass { var key: String? } extension MyClass: Equatable { static func == (lhs: MyClass, rhs: MyClass) -> Bool { return MyClass.key == MyClass.key } } 

这在Swift 3中为我工作没有扩展:

 struct MyClass: Equatable { let title: String public static func ==(lhs: MyClass, rhs: MyClass) -> Bool { return lhs.title == rhs.title } }