ios 在 Swift 数组中查找第一个元素匹配条件(例如 EKSource)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33795654/
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 first element matching condition in Swift array (e.g. EKSource)
提问by Drux
I would like to find the first EKSource
of type EKSourceType.Local
with a "single"-line expression in Swift. Here is what I currently have:
我想在 Swift 中找到第一个带有“单”行表达式EKSource
的类型EKSourceType.Local
。这是我目前拥有的:
let eventSourceForLocal =
eventStore.sources[eventStore.sources.map({ let local = eventStore.sources.first(where: {if let index = eventStore.sources.indexOf({ extension SequenceType {
func find(@noescape predicate: (Self.Generator.Element) throws -> Bool) rethrows -> Self.Generator.Element? {
for element in self {
if try predicate(element) {
return element
}
}
return nil
}
}
let eventSourceForLocal = eventStore.sources.find({ let local = eventStore.sources.filter{if let firstMatch = yourArray.first{let arr = [0,1,2,3]
let result = arr.lazy.map { print(""); return public extension Sequence {
func find(predicate: (Iterator.Element) throws -> Bool) rethrows -> Iterator.Element? {
for element in self {
if try predicate(element) {
return element
}
}
return nil
}
}
}.first(where: { if let index = Singleton.sharedInstance.selectedTaskRelatedArray.firstIndex(where: { ##代码##.id == lookupId }) {
print("Found at \(index)")
} else {
print("Not found")
}
== 2 })
print(result) // 3x then 2
.id == lookupId} {
print("found it: \(firstMatch)")
} else {
print("nothing found :(")
}
.sourceType == .Local}.first
.sourceType == .Local })
.sourceType == .Local }) {
let eventSourceForLocal = eventStore.sources[index]
}
.sourceType == .Local})
.sourceType })
.indexOf(EKSourceType.Local)!]
Is there a better way of doing this (such as without mapping and/or with a generic version of find
)?
有没有更好的方法来做到这一点(例如没有映射和/或使用 的通用版本find
)?
回答by Bueno
Alternatively in Swift3 you could use:
或者在 Swift3 中,您可以使用:
##代码##回答by Nate Cook
There's a version of indexOf
that takes a predicate closure - use it to find the index of the first local source (if it exists), and then use that index on eventStore.sources
:
有一个版本indexOf
采用谓词闭包 - 使用它来查找第一个本地源的索引(如果存在),然后在 上使用该索引eventStore.sources
:
Alternately, you could add a generic find
method via an extension on SequenceType
:
或者,您可以find
通过扩展添加一个通用方法SequenceType
:
(Why isn't this there already?)
(为什么这里没有?)
回答by matt
I don't understand why you're using map
at all. Why not use filter
? You will then end up with all the local sources, but in actual fact there will probably be only one, or none, and you can readily find out by asking for the first one (it will be nil
if there isn't one):
我完全不明白你为什么要使用map
。为什么不使用filter
?然后你会得到所有的本地资源,但实际上可能只有一个,或者没有,你可以通过询问第一个来轻松找到(nil
如果没有,就会找到):
回答by budidino
Swift 4solution that also handles the situation when there are no elements in your array that match your condition:
Swift 4解决方案还可以处理数组中没有与您的条件匹配的元素的情况:
##代码##回答by eonist
Let's try something more functional:
让我们尝试一些更实用的东西:
##代码##Whats cool about this?
You get access to element or i while you search. And it's functional.
这有什么好?
您可以在搜索时访问 element 或 i 。它是功能性的。
回答by Tyler Sheaffer
For Swift 3 you'll need to make a few small changes to Nate's answer above. Here's the Swift 3 version:
对于 Swift 3,您需要对上面 Nate 的答案进行一些小的更改。这是 Swift 3 版本:
##代码##Changes: SequenceType
> Sequence
, Self.Generator.Element
> Iterator.Element
变化:SequenceType
> Sequence
, Self.Generator.Element
>Iterator.Element
回答by Gurjinder Singh
Swift 5If you want to find out from Array of Model then speciyfy $0.keyTofoundotherwise use $0
Swift 5如果你想从 Array of Model 中找出然后指定$0.keyTofound否则使用$0
##代码##