删除特定的数组元素,等于字符串 - Swift [英] Remove Specific Array Element, Equal to String - Swift

查看:117
本文介绍了删除特定的数组元素,等于字符串 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个元素等于给定的字符串,是否没有简单的方法从数组中删除它?解决方法是找到要删除的数组元素的索引,然后是 removeAtIndex ,或创建一个新数组,其中追加所有不相等的元素到给定的字符串。但是没有更快的方法吗?

Is there no easy way to remove a specific element from an array, if it is equal to a given string? The workarounds are to find the index of the element of the array you wish to remove, and then removeAtIndex, or to create a new array where you append all elements that are not equal to the given string. But is there no quicker way?

推荐答案

您可以使用filter()过滤数组,如下所示

var myArrayOfStrings = ["Hello","Playground","World"]

myArrayOfStrings = myArrayOfStrings.filter{$0 != "Hello"}

print(myArrayOfStrings)   // "[Playground, World]"

这篇关于删除特定的数组元素,等于字符串 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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