cocoa - Swift 中 Array<Prototocol> 中对象的索引

标签 cocoa swift

我想获取当前主题的索引。 我的主题列表按以下方式声明

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 知道我做错了什么吗?

最佳答案

== 要求两个对象有一个等价运算符(想想 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/

相关文章:

ios - 自定义iOS动态框架: Swift class is imported but can't be initialized

objective-c - 使用 typedef 的不兼容 block 指针类型

swift - 使用 UILabel 和 UITextdfield 创建组件

swift - 如何在 Swift 中将字符串 UTC 日期转换为 NSDate

objective-c - 如何在/Library/Application Support中创建目录

ios - WKImage 到数据总是返回 nil

ios - 无法在 Objective-C 项目中使用 RAMAnimatedTabBarController?

xcode - 如何将多个应用程序作为 mac os x 的一个存档/ bundle /应用程序进行分发

iPhone:使用简单的键/值字符串创建二进制 Plist 的最快方法

objective-c - Objective-C 的 "stringWithFormat:"方法发生了什么?