为了与比较的你正在做我应该使用hasPrefix
或range
方法的类型更加明确:
import Foundation
let foo = "test"
let arrayValues = ["Testy", "tester", "Larry", "testing", "untested"]
// hasPrefix is case-sensitive
let filterArray = arrayValues.filter {
$0.hasPrefix(foo)
}
print(filterArray) // -> "["tester", "testing"]\n"
/* Range can do much more, including case-insensitive.
The options [.anchored, .caseInsensitive] mean the search will
only allow a range that starts at the startIndex and
the comparison will be case-insensitive
*/
let filterArray2 = arrayValues.filter {
// filters the element if foo is not found case-insensitively at the start of the element
$0.range(of: foo, options: [.anchored, .caseInsensitive]) != nil
}
print(filterArray2) // -> "["Testy", "tester", "testing"]\n"
如何定义你的arrayValues? –
var arrayValues = JSON [“array”] as? [字符串],我正在下载这个表单API –
而'print(arrayValues)'输出是? –