如何使用swift删除数组中包含字母的元素 [英] how to delete an element that contains a letter in array using swift

查看:118
本文介绍了如何使用swift删除数组中包含字母的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难弄清楚这个话题。与主题一样,如何删除数组中包含字母的元素。

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)

我尝试了此堆栈溢出给出的多个选项。
如何从数组中删除元素在Swift

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

我想我碰壁了。

推荐答案

您是否尝试过

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

这篇关于如何使用swift删除数组中包含字母的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆