2015-10-05 48 views
-1

我試過的是使用for-in循環,但是隻循環到最後一個值,並給出rangeOfString的值。爲我想要的所有值編寫rangeOfString會很多,但如果需要,我不介意。我只是想知道是否可以使用數組。是否有可能從數組中的一組項目中獲取rangeOfString?

while true { 
     let attempts = ["hello","goodbye"] 
     for attempt in attempts{ 

      tru = attempt 




     let range = text.rangeOfString("hello", options: [], range: searchRange) 

回答

0

您可以使用map

let text: NSString = "hello goodbye" 
let attempts = ["hello", "goodbye"] 
let ranges = attempts.map { text.rangeOfString($0, options: NSStringCompareOptions(rawValue: 0)) } 
相關問題