如果包含字符

时间:2019-02-24 02:41:04

标签: ios arrays swift

如果字符串与UITextField中的字符相联系,如何简单地从数组中删除字符串?

例如:如果UITextField包含字母:“ Ap ”,则数组将仅包含“ Ap ple, Ap p, Ap 如此”,但不包括“ Al,Ben,Culo”。

我的数组:

let array = ["Audi", "BMW", "Lamborghini", "Toyota", "Mazda", "McLaren", "Volkswagen", "Ford", "Aston Martin", "Bentley"]

代码:

func setValue() {

        if let index = array.index(of: exerciseNameTextField.text!) {
            array.remove(at: index)
        }

´
    }

1 个答案:

答案 0 :(得分:0)

You can try:

let result = array.filter { $0.contains(exerciseNameTextField.text!) }