Dru*_*rux 34 ios eventkit swift ios9 swift2
我想在Swift中找到第一个带有"单"线表达式EKSource的类型EKSourceType.Local.这是我目前拥有的:
let eventSourceForLocal =
eventStore.sources[eventStore.sources.map({ $0.sourceType })
.indexOf(EKSourceType.Local)!]
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法(例如没有映射和/或通用版本find)?
Bue*_*eno 83
或者在Swift3中你可以使用:
let local = eventStore.sources.first(where: {$0.sourceType == .Local})
Run Code Online (Sandbox Code Playgroud)
Nat*_*ook 37
有一个版本indexOf需要谓词闭包 - 使用它来查找第一个本地源的索引(如果存在),然后使用该索引eventStore.sources:
if let index = eventStore.sources.indexOf({ $0.sourceType == .Local }) {
let eventSourceForLocal = eventStore.sources[index]
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以find通过以下扩展名添加通用方法SequenceType:
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({ $0.sourceType == .Local })
Run Code Online (Sandbox Code Playgroud)
(为什么不在那里?)
mat*_*att 19
我不明白你为什么要使用它map.为什么不用filter?然后你会得到所有本地资源,但实际上可能只有一个,或者没有,你可以通过询问第一个来找到答案(nil如果没有的话):
let local = eventStore.sources.filter{$0.sourceType == .Local}.first
Run Code Online (Sandbox Code Playgroud)
bud*_*ino 13
Swift 4解决方案,当数组中没有符合您条件的元素时,它也会处理这种情况:
if let firstMatch = yourArray.first{$0.id == lookupId} {
print("found it: \(firstMatch)")
} else {
print("nothing found :(")
}
Run Code Online (Sandbox Code Playgroud)
Swift 5如果您想从模型数组中查找,请指定$0.keyTofound否则使用$0
if let index = listArray.firstIndex(where: { $0.id == lookupId }) {
print("Found at \(index)")
} else {
print("Not found")
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29400 次 |
| 最近记录: |