gpt4 book ai didi

cocoa - Swift 中 Array 中对象的索引

转载 作者:可可西里 更新时间:2023-11-01 00:53:56 26 4
gpt4 key购买 nike

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

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)
}

这一切都直接来自 iTunes 书店免费提供的 Apple 引用指南。

请注意,这些函数是在模块范围内定义的(即,在任何类和/或结构声明之外)

关于cocoa - Swift 中 Array<Prototocol> 中对象的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24085254/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com