我有2个班级:
- Favourite
+ key
+ showKey
- Show
+ key
我有一个favouriteShows
[Favourite]
的数组
我有一个展示对象show
要检查节目的键是否属于favouriteShows,我会这样做:
if favouriteShows.contains(where: {$0.showKey == show.key}) {
...
}
但我也想确定哪个是showKey的最爱。
像let favouriteIndex = favouriteShows.contains(where: {$0.showKey == show.key})
答案 0 :(得分:1)
您正在寻找index(where:)
guard let favouriteIndex = favouriteShows.index(where: {$0.showKey == show.key}) else {
// no favourite matched
return
}
// use favouriteIndex
答案 1 :(得分:0)
或者,您可以按如下方式枚举favouriteShows
数组:
for (ndx, sh) in favouriteShows.enumerated() {
if sh.showKey == show.key {
// ndx contains the index at this point
}
}