how to delete an element that contains a letter in array using swift

放肆的年华 提交于 2020-01-07 09:25:10

问题


I am having trouble trying to figure this topic out. Like the topic, How do I delete an element that contains a letter in Array. This is the code I have so far.

let newline = "\n"
    let task = Process()
    task.launchPath = "/bin/sh"
    task.arguments = ["-c", "traceroute -nm 18 -q 1 8.8.8.8"]

    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) as! String
    var array = output.components(separatedBy: "  ")
    array = array.filter(){$0 != "m"}



    print(array, newline)

I have tried multiple options given by this stack overflow. How to remove an element from an array in Swift

I think I have hit a wall.


回答1:


Have you tried

array = array.filter({ !$0.contains("m") })


来源:https://stackoverflow.com/questions/41881406/how-to-delete-an-element-that-contains-a-letter-in-array-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!