let array = ["apple", "dog", "log"]
let indexes = array.enumerated().filter {
$0.element.contains("og")
}.map{$0.offset}
print(indexes)
Swift 2:
let array = ["apple", "dog", "log"]
let indexes = array.enumerate().filter {
$0.element.containsString("og")
}.map{$0.index}
print(indexes)
148
任何此解决方案都适合我
这是我对Swift 4的解决方案:
let monday = Day(name: "M")
let tuesday = Day(name: "T")
let friday = Day(name: "F")
let days = [monday, tuesday, friday]
let index = days.index(where: {
//important to test with === to be sure it's the same object reference
$0 === tuesday
})
2
如果你还在使用Swift 1.x
然后尝试,
let testArray = ["A","B","C"]
let indexOfA = find(testArray, "A")
let indexOfB = find(testArray, "B")
let indexOfC = find(testArray, "C")
2
Swift 4.如果您的数组包含[String:AnyObject]类型的元素 . 所以要找到元素的索引使用下面的代码
var array = [[String: AnyObject]]()// Save your data in array
let objectAtZero = array[0] // get first object
let index = (self.array as NSArray).index(of: objectAtZero)
let userId = 20
if let index = array.index(where: { (dict) -> Bool in
return dict.id == userId // Will found index of matched id
}) {
print("Index found")
}
77
Swift 2.1
var array = ["0","1","2","3"]
if let index = array.indexOf("1") {
array.removeAtIndex(index)
}
print(array) // ["0","2","3"]
Swift 3
var array = ["0","1","2","3"]
if let index = array.index(of: "1") {
array.remove(at: index)
}
array.remove(at: 1)
var languages = ["Swift", "Objective-C"]
contains(languages, "Swift") == true
contains(languages, "Java") == false
contains([29, 85, 42, 96, 75], 42) == true
if (contains(languages, "Swift")) {
// Use contains in these cases, instead of find.
}
16 回答
虽然
indexOf()
完美无缺, it only returns one index.我正在寻找一种优雅的方法来获取满足某些条件的元素的索引数组 .
以下是它的完成方式:
Swift 3:
Swift 2:
任何此解决方案都适合我
这是我对Swift 4的解决方案:
如果你还在使用Swift 1.x
然后尝试,
Swift 4.如果您的数组包含[String:AnyObject]类型的元素 . 所以要找到元素的索引使用下面的代码
或者如果你想在Dictionary的基础上找到索引 . 这里数组包含Model类的对象,我匹配id属性 .
Swift 2.1
Swift 3
您还可以使用函数库Dollar在数组上执行indexOf http://www.dollarswift.org/#indexof-indexof
Swift 4
对于参考类型:
在Swift 2(使用Xcode 7)中,
Array
包含CollectionType协议提供的indexOf方法 . (实际上,两个indexOf
方法 - 一个使用相等来匹配一个参数,一个another使用一个闭包 . )在Swift 2之前,像集合这样的泛型类型没有办法为从它们派生的具体类型(如数组)提供方法 . 因此,在Swift 1.x中,"index of"是一个全局函数......它也被重命名,所以在Swift 1.x中,该全局函数被称为
find
.也可以(但不是必须)使用
NSArray
中的indexOfObject
方法...或者任何其他更复杂的基础搜索方法,它在Swift标准库中没有等价物 . 只需import Foundation
(或另一个传递Foundation的模块),将Array
强制转换为NSArray
,并且可以在NSArray
上使用多种搜索方法 .在 Swift 4 中,如果遍历DataModel数组,请确保您的数据模型符合Equatable Protocol,实现lhs = rhs方法,然后才能使用".index(of" . 例如
然后,
对于SWIFT 3,您可以使用简单的功能
这将给出数字位置,所以你可以使用喜欢
希望有用
你可以
filter
一个带闭包的数组:你可以数一个数组:
因此,您可以通过组合这些来确定数组是否包含您的元素:
如果你想找到这个位置,我看不到花哨的方式,但你肯定可以这样做:
EDIT
虽然我们're at it, for a fun exercise let'扩展
Array
以使用find
方法:现在我们可以这样做:
对于自定义类,您需要实现Equatable协议 .
Swift 4.2
例1
例题
Update for Swift 2:
Swift 1:
如果你只想检查数组中是否包含一个元素,也就是说,只需获得一个布尔指示符,请使用
contains(sequence, element)
而不是find(array, element)
:见下面的例子:
由于swift在某些方面比面向对象更具功能性(并且Arrays是结构,而不是对象),因此使用函数“find”对数组进行操作,该函数返回一个可选值,因此请准备好处理nil值:
Update for Swift 2.0:
使用Swift 2.0不再支持旧的
find
功能了!使用Swift 2.0,
Array
获得了使用CollectionType
(Array
implements)扩展中定义的函数查找元素索引的能力:此外,查找满足谓词的数组中的第一个元素是由
CollectionType
的另一个扩展支持的:请注意,这两个函数返回可选值,如之前
find
所做的那样 .Update for Swift 3.0:
请注意indexOf的语法已更改 . 对于符合
Equatable
的物品,您可以使用:有关该方法的详细文档,请访问https://developer.apple.com/reference/swift/array/1689674-index
对于不符合
Equatable
的数组项,您需要使用index(where:)
:Update for Swift 4.2:
使用Swift 4.2时,不再使用
index
,但为了更好地说明,将其分为firstIndex
和lastIndex
. 因此,取决于您是否正在寻找项目的第一个或最后一个索引:我认为值得一提的是,使用引用类型(
class
),您可能希望执行身份比较,在这种情况下,您只需要在谓词闭包中使用===
identity运算符:Swift 4 / Swift 3:
请注意,上面的语法使用尾随闭包语法,相当于:
Swift 2 -
index
函数语法曾经是:*注意实现
Equatable
的相关且有用的comment关于class
类型,您需要考虑是否应使用===
(身份运算符)或==
(相等运算符)进行比较 . 如果您决定使用==
进行匹配,则可以使用其他人建议的方法(people.index(of: person1)
) .