我尝试过的是使用for-in循环,但是它只循环到最后一个值并为我提供该值的rangeOfString。为我想要的所有值编写rangeOfString会很多,但如果需要,我不介意。我只是想知道是否可以使用数组。
while true {
let attempts = ["hello","goodbye"]
for attempt in attempts{
tru = attempt
let range = text.rangeOfString("hello", options: [], range: searchRange)
答案 0 :(得分:0)
您可以使用map
:
let text: NSString = "hello goodbye"
let attempts = ["hello", "goodbye"]
let ranges = attempts.map { text.rangeOfString($0, options: NSStringCompareOptions(rawValue: 0)) }