ios 过滤字符串数组,包括“喜欢”条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34511918/
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
Filter array of strings, including "like" condition
提问by Roi Mulia
If my main array is ["Hello","Bye","Halo"]
, and I'm searching for "lo"
, it will filter the array only to ["Hello", "Halo"]
.
如果我的主数组是["Hello","Bye","Halo"]
,并且我正在搜索"lo"
,它将仅将数组过滤为["Hello", "Halo"]
。
This is what I've tried:
这是我尝试过的:
let matchingTerms = filter(catalogNames) {
Type of expression is ambiguous without more context
.rangeOfString(self.txtField.text!, options: .CaseInsensitiveSearch) != nil
}
It throws
它抛出
let arr = ["Hello","Bye","Halo"]
let filtered = arr.filter { let terms = ["Hello","Bye","Halo"]
var filterdTerms = [String]()
func filterContentForSearchText(searchText: String) {
filterdTerms = terms.filter { term in
return term.lowercased().contains(searchText.lowercased())
}
}
filterContentForSearchText(searchText: "Lo")
print(filterdTerms)
.contains("lo") }
print(filtered)
Any suggestions?
有什么建议?
回答by luk2302
Use contains
instead:
使用contains
来代替:
["Hello", "Halo"]
Output
输出
["Hello", "Halo"]
[“你好”、“晕”]
Thanks to @user3441734 for pointing out that functionality is of course only available when you import Foundation
感谢@user3441734 指出该功能当然只有在您 import Foundation
回答by Ashok R
In Swift 3.0
在 Swift 3.0 中
let catalogNames = [ "Hats", "Coats", "Trousers" ]
let searchCatalogName = "Hats"
let filteredCatalogNames = catalogNames.filter { catalogName in
return catalogName.localizedCaseInsensitiveContains(searchCatalogName)
}
print(filteredCatalogNames)
Output
输出
let brands = ["Apple", "FB", "Google", "Microsoft", "Amazon"]
let b = brands.filter{(x) -> Bool in
(x.lowercased().range(of: "A".lowercased()) != nil)
}
print(b) //["Apple", "Amazon"]
回答by Roshna D'souza
Swift 3.1
斯威夫特 3.1
extension String {
func contains(string: String)->Bool {
guard !self.isEmpty else {
return false
}
var s = self.characters.map{ import Foundation
let catalogNames = [ "Hats", "Coats", "Trousers" ]
let matchingTerms = catalogNames.filter {
##代码##.rangeOfString(self.txtField.text!, options: .CaseInsensitiveSearch).location != NSNotFound
}
}
let c = string.characters.map{ ##代码## }
repeat {
if s.startsWith(c){
return true
} else {
s.removeFirst()
}
} while s.count > c.count - 1
return false
}
}
let arr = ["Hello","Bye","Halo"]
let filtered = arr.filter { ##代码##.contains("lo") }
print(filtered) // ["Hello", "Halo"]
"a".contains("alphabet") // false
"alphabet".contains("") // true
回答by u5150587
my try...
我的尝试...
##代码##回答by user3441734
with help of String extension you can use pure Swift solution (without import Foundation). I didn't check the speed, but it shouldn't be worse as the foundation equivalent.
在字符串扩展的帮助下,您可以使用纯 Swift 解决方案(无需导入 Foundation)。我没有检查速度,但它应该不会比基础等价物差。
##代码##回答by Oliver Atkinson
You also need to compare to NSNotFound. The documentation for rangeOfString:options: says:
您还需要与 NSNotFound 进行比较。rangeOfString:options: 的文档说:
An NSRange structure giving the location and length in the receiver of the first occurrence of aString, modulo the options in mask. Returns {NSNotFound, 0} if aString is not found or is empty (@"").
##代码##一个 NSRange 结构给出了第一次出现 aString 在接收器中的位置和长度,以掩码中的选项为模。如果 aString 未找到或为空 (@""),则返回 {NSNotFound, 0}。