一个变量中有多个RangeOfString

时间:2016-11-16 08:54:17

标签: swift

我有什么解决方案可以像下面的代码那样做吗?

let tmp: NSString = data.status
        let tmp1: NSString = data.process
 let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch) && tmp1.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
        return range.location != NSNotFound

我这样做是因为我想搜索多个字段。 谢谢

2 个答案:

答案 0 :(得分:0)

enter image description here您可以试试这个

if data.status.lowercased().contains(searchText.lowercased()) || data.process.contains(searchText) {
    return true
}else {
    return false
} 

答案 1 :(得分:0)

如果您要查找范围,则应使用RegularExpression代替

import Foundation

let pattern = "search"

let regexp = try! NSRegularExpression(pattern: pattern, options: NSRegularExpression.Options())
let string = "String to search in search"
regexp.enumerateMatches(in: string, options: NSRegularExpression.MatchingOptions(), range: NSMakeRange(0, string.characters.count), using:  { (result, flags, stop) in

    if result?.range != nil {
        print("Range is \(result!.range.location)-\(result!.range.length)")
    }
})