ios 在数组中查找具有属性的对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26073331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find Object with Property in Array
提问by derdida
is there a possibility to get an object from an array with an specific property? Or do i need to loop trough all objects in my array and check if an property is the specific i was looking for?
是否有可能从具有特定属性的数组中获取对象?或者我是否需要遍历数组中的所有对象并检查属性是否是我正在寻找的特定属性?
edit: Thanks for given me into the correct direction, but i have a problem to convert this.
编辑:谢谢你给我正确的方向,但我有一个问题来转换这个。
// edit again: A ok, and if there is only one specific result? Is this also a possible method do to that?
// 再次编辑:A ok,如果只有一个特定结果?这也是一种可能的方法吗?
let imageUUID = sender.imageUUID
let questionImageObjects = self.formImages[currentSelectedQuestion.qIndex] as [Images]!
// this is working
//var imageObject:Images!
/*
for (index, image) in enumerate(questionImageObjects) {
if(image.imageUUID == imageUUID) {
imageObject = image
}
}
*/
// this is not working - NSArray is not a subtype of Images- so what if there is only 1 possible result?
var imageObject = questionImageObjects.filter( { return let imageObject = questionImageObjects.filter{ questionImageObjects.first(where: { let index = questionImageObjects.indexOf({extension CollectionType {
func find(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Self.Generator.Element? {
return try indexOf(predicate).map({self[questionImageObjects.find({struct User {
var firstName: String?
var lastName: String?
}
let users = [User(firstName: "John", lastName: "Doe"), User(firstName: "Bill", lastName: "Clinton"), User(firstName: "John", lastName: "Travolta")];
let johns = users.filter( { return class Point{
var x:Int
var y:Int
init(x:Int, y:Int){
self.x = x
self.y = y
}
}
var p1 = Point(x:1, y:2)
var p2 = Point(x:2, y:3)
var p3 = Point(x:1, y:4)
var points = [p1, p2, p3]
// Find the first object with given property
let firstMatchingPoint = points.first{if arrayTicketsListing.contains({ // TicketsStatusList class
class TicketsStatusList {
internal var status_id: String
internal var status_name: String
init(){
status_id = ""
status_name = ""
}
}
.status_id == "2" }) {
let ticketStatusObj: TicketsStatusList = arrayTicketsListing[arrayTicketsListing.indexOf({ ##代码##.status_id == "2" })!]
print(ticketStatusObj.status_name)
}
.x == 1}
// Find all objects with given property
let allMatchingPoints = points.filter{##代码##.x == 1}
.firstName == "John" } )
.imageUUID == imageUUID})
]})
}
}
.imageUUID == imageUUID})
.imageUUID == imageUUID })
.imageUUID == imageUUID }.first
.imageUUID == imageUUID } )
回答by Rob Napier
// this is not working - NSArray is not a subtype of Images- so what if there is only 1 possible result?
// 这不起作用 - NSArray 不是图像的子类型 - 那么如果只有 1 个可能的结果怎么办?
You have no way to prove at compile-time that there is only one possible result on an array. What you're actually asking for is the firstmatching result. The easiest (though not the fastest) is to just take the first element of the result of filter:
您无法在编译时证明数组中只有一种可能的结果。您实际要求的是第一个匹配结果。最简单的(虽然不是最快的)是只取过滤器结果的第一个元素:
##代码##imageObject
will now be an optional of course, since it's possible that nothing matches.
imageObject
现在当然是可选的,因为可能没有任何匹配项。
If searching the whole array is time consuming, of course you can easily create a firstMatching
function that will return the (optional) first element matching the closure, but for short arrays this is fine and simple.
如果搜索整个数组很耗时,当然您可以轻松创建一个firstMatching
函数,该函数将返回与闭包匹配的(可选)第一个元素,但对于短数组,这很好且简单。
As charles notes, in Swift 3 this is built in:
正如查尔斯所说,在 Swift 3 中,这是内置的:
##代码##回答by nschum
Edit 2016-05-05:Swift 3 will include first(where:)
.
编辑 2016-05-05:Swift 3 将包括first(where:)
.
In Swift 2, you can use indexOf
to find the index of the first array element that matches a predicate.
在 Swift 2 中,您可以使用indexOf
来查找与谓词匹配的第一个数组元素的索引。
This is bit faster compared to filter
since it will stop after the first match. (Alternatively, you could use a lazy
sequence.)
这比filter
它快一点,因为它会在第一场比赛后停止。(或者,您可以使用lazy
序列。)
However, it's a bit annoying that you can only get the index and not the object itself. I use the following extension for convenience:
但是,您只能获取索引而不能获取对象本身,这有点烦人。为方便起见,我使用以下扩展名:
##代码##Then the following works:
然后以下工作:
##代码##回答by Antonio
Yes, you can use the filter
method which takes a closure where you can set your logical expression.
是的,您可以使用filter
带有闭包的方法,您可以在其中设置逻辑表达式。
Example:
例子:
##代码##Note that filter
returns an array containing all items satisfying the logical expression.
请注意,filter
返回一个包含满足逻辑表达式的所有项目的数组。
More info in the Library Reference
库参考中的更多信息
回答by Kai-jie Ke
回答by Kiran Jasvanee
Here is other way to fetch particular object by using object property to search an object in array.
这是通过使用对象属性在数组中搜索对象来获取特定对象的另一种方法。
##代码##Whereas, my arrayTicketsListing is [TicketsStatusList]
contains objects of TicketsStatusList
class.
而我的 arrayTicketsListing[TicketsStatusList]
包含TicketsStatusList
类的对象。