Ios 在数组中查找对象?
Swift在underline.js中是否有类似的内容 我有一个类型为Ios 在数组中查找对象?,ios,swift,Ios,Swift,Swift在underline.js中是否有类似的内容 我有一个类型为T的结构数组,我想检查数组是否包含name属性等于Foo的结构对象 尝试使用find()和filter(),但它们仅适用于基本类型,例如String或Int。引发有关不符合公平协议或类似协议的错误。使用包含: var yourItem:YourType! if contains(yourArray, item){ yourItem = item } 或者,您可以尝试Martin在评论中指出的内容,然后再试一次。您可以
T
的结构数组,我想检查数组是否包含name
属性等于Foo
的结构对象
尝试使用
find()
和filter()
,但它们仅适用于基本类型,例如String
或Int
。引发有关不符合公平
协议或类似协议的错误。使用包含
:
var yourItem:YourType!
if contains(yourArray, item){
yourItem = item
}
或者,您可以尝试Martin在评论中指出的内容,然后再试一次。您可以过滤数组,然后只选择第一个元素,如图所示
在
或者定义自定义扩展名
extension Array {
// Returns the first element satisfying the predicate, or `nil`
// if there is no matching element.
func findFirstMatching<L : BooleanType>(predicate: T -> L) -> T? {
for item in self {
if predicate(item) {
return item // found
}
}
return nil // not found
}
}
在Swift 3中,您可以先使用现有的
方法(其中:)
方法
(as):
FWIW,如果您不想使用自定义函数或扩展,您可以:
let array = [ .... ]
if let found = find(array.map({ $0.name }), "Foo") {
let obj = array[found]
}
这将首先生成name
数组,然后从中找到find
如果您拥有庞大的阵列,则可能需要执行以下操作:
if let found = find(lazy(array).map({ $0.name }), "Foo") {
let obj = array[found]
}
或者可能:
if let found = find(lazy(array).map({ $0.name == "Foo" }), true) {
let obj = array[found]
}
您可以将
数组
上提供的索引
方法与谓词()一起使用
对于您的具体示例,这将是:
Swift 5.0
if let i = array.firstIndex(where: { $0.name == "Foo" }) {
return array[i]
}
if let i = array.index(where: { $0.name == Foo }) {
return array[i]
}
if let i = array.indexOf({ $0.name == Foo }) {
return array[i]
}
if let index = array.index(where: { $0.name == "Foo" }) {
return array[index]
}
Swift 3.0
if let i = array.firstIndex(where: { $0.name == "Foo" }) {
return array[i]
}
if let i = array.index(where: { $0.name == Foo }) {
return array[i]
}
if let i = array.indexOf({ $0.name == Foo }) {
return array[i]
}
if let index = array.index(where: { $0.name == "Foo" }) {
return array[index]
}
Swift 2.0
if let i = array.firstIndex(where: { $0.name == "Foo" }) {
return array[i]
}
if let i = array.index(where: { $0.name == Foo }) {
return array[i]
}
if let i = array.indexOf({ $0.name == Foo }) {
return array[i]
}
if let index = array.index(where: { $0.name == "Foo" }) {
return array[index]
}
Swift 3.0
if let i = array.firstIndex(where: { $0.name == "Foo" }) {
return array[i]
}
if let i = array.index(where: { $0.name == Foo }) {
return array[i]
}
if let i = array.indexOf({ $0.name == Foo }) {
return array[i]
}
if let index = array.index(where: { $0.name == "Foo" }) {
return array[index]
}
Swift 2.1
swift 2.1现在支持在对象属性中进行过滤。您可以根据结构或类的任何值筛选数组,这是一个示例
for myObj in myObjList where myObj.name == "foo" {
//object with name is foo
}
或
SWIFT 5 检查元素是否存在
if array.contains(where: {$0.name == "foo"}) {
// it exists, do something
} else {
//item could not be found
}
获取元素
if let foo = array.first(where: {$0.name == "foo"}) {
// do something with foo
} else {
// item could not be found
}
if let foo = array.enumerated().first(where: {$0.element.name == "foo"}) {
// do something with foo.offset and foo.element
} else {
// item could not be found
}
if let fooOffset = array.firstIndex(where: {$0.name == "foo"}) {
// do something with fooOffset
} else {
// item could not be found
}
获取元素及其偏移量
if let foo = array.first(where: {$0.name == "foo"}) {
// do something with foo
} else {
// item could not be found
}
if let foo = array.enumerated().first(where: {$0.element.name == "foo"}) {
// do something with foo.offset and foo.element
} else {
// item could not be found
}
if let fooOffset = array.firstIndex(where: {$0.name == "foo"}) {
// do something with fooOffset
} else {
// item could not be found
}
获取偏移量
if let foo = array.first(where: {$0.name == "foo"}) {
// do something with foo
} else {
// item could not be found
}
if let foo = array.enumerated().first(where: {$0.element.name == "foo"}) {
// do something with foo.offset and foo.element
} else {
// item could not be found
}
if let fooOffset = array.firstIndex(where: {$0.name == "foo"}) {
// do something with fooOffset
} else {
// item could not be found
}
Swift 2或更高版本 您可以将
indexOf
和map
组合在一行中编写“查找元素”函数
let array = [T(name: "foo"), T(name: "Foo"), T(name: "FOO")]
let foundValue = array.indexOf { $0.name == "Foo" }.map { array[$0] }
print(foundValue) // Prints "T(name: "Foo")"
首先使用
filter
+看起来更干净,但是filter
会计算数组中的所有元素indexOf
+map
看起来很复杂,但在数组中找到第一个匹配项时,计算就会停止。这两种方法各有利弊。Swift 3
if yourArray.contains(item) {
//item found, do what you want
}
else{
//item not found
yourArray.append(item)
}
您可以在Swift 3中使用索引(其中:)
func index(where predicate: @noescape Element throws -> Bool) rethrows -> Int?
范例
if let i = theArray.index(where: {$0.name == "Foo"}) {
return theArray[i]
}
Swift 3
if yourArray.contains(item) {
//item found, do what you want
}
else{
//item not found
yourArray.append(item)
}
如果需要对象,请使用:
array.first{$0.name == "Foo"}
(如果您有多个名为“Foo”的对象,则first
将从未指定的顺序返回第一个对象)另一种访问数组的方法。索引(of:Any)是通过声明您的对象
import Foundation
class Model: NSObject { }
对于Swift,请使用Lo Dash或下划线.js:
import Dollar
let found = $.find(array) { $0.name == "Foo" }
Swift 3
if yourArray.contains(item) {
//item found, do what you want
}
else{
//item not found
yourArray.append(item)
}
Swift 3:
您可以使用Swifts内置功能查找数组中的自定义对象
首先,必须确保自定义对象符合:equalable协议
class Person : Equatable { //<--- Add Equatable protocol
let name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
//Add Equatable functionality:
static func == (lhs: Person, rhs: Person) -> Bool {
return (lhs.name == rhs.name)
}
}
对于Swift 3
let index = array.index(where: {$0.name == "foo"})
Swift 4
实现这一目标的另一种方法
使用过滤功能
if let object = elements.filter({ $0.title == "title" }).first {
print("found")
} else {
print("not found")
}
例如,如果我们有一个数字数组:
let numbers = [2, 4, 6, 8, 9, 10]
我们可以找到这样的第一个奇数:
let firstOdd = numbers.index { $0 % 2 == 1 }
这将返回4作为可选整数,因为第一个奇数(9)位于索引4。这将只返回布尔值吗?我也需要获取对象,而不仅仅是检查是否在数组中。这假设item
与数组中的item的类型相同。然而,我所拥有的只是view.annotation.title
中的一个标题。我需要按此标题比较数组中的项。类似于if contains(youraray,view.annotation.title){//code goes here}
。Martin在注释中向您展示了另一种方法。检查他提供的链接。这可能是您正在查找的:。为什么不转换为nsdictionary并搜索我认为在Swift 2.0中不再提供find。。我把一些1.2代码转换成Swift 2.0,它说用IndexOf代替。这更好。我将此标记为答案,因为它总体上看起来更简单,并且不需要创建自定义函数。从Swift 2.0开始,您可以使用:array.indexOf({$0.name==“Foo”})从Swift 3.0开始,您可以使用:array.first(其中:{$0.name==“Foo”})如果需要对象,我如何检查$0.name包含字符串“Foo”?答案是关于精确匹配。我需要一个字符串。有人能给出它的语法吗?是的,这只适用于swift 2.0。抱歉,我应该提到它。@user3799504$0的意思?$0是闭包的第一个参数的缩写。在本例中,它指的是self.generator.element或数组的每个元素。请看:Swift 3如何?就效率而言,它与数组.惰性.过滤器(谓词).first
相比如何?效率有多高。对于小型阵列来说是懒惰的?@PatNiemeyer:我不知道,您必须测量性能并进行比较。@PatNiemeyer上述解决方案肯定会更有效,尽管差异可能不会很大。1.filter
的复杂性总是O(n)
而在findFirstMatching
中,它只在最坏的情况下出现(当您要查找的元素是数组中的最后一个或根本不在数组中时)。2. filter
创建一个全新的筛选元素数组,而findFirstMatching
只返回请求的元素。在Swift 3中,我从非协议继承错误,非类类型“Bool”和未声明的类型“T”用于此扩展方法。@Isuru:这个答案很旧,引用了旧的Swift版本。在Swift 3中,你不再需要自定义扩展方法,我已经相应地更新了答案。很好的Swift风格的答案!谢谢你是我的保护程序这已经清理了我的代码很多如何检查多个条件说我需要检查数组$0.name==“foo”
是否执行一个操作和$0.name==“boo”
执行另一个操作这对我的帮助比接受更多