如何从Swift中删除给定值的元素

时间:2021-05-08 21:21:32

I want to remove all elements of value x from an array that contains x, y and z elements

我想从包含x、y和z元素的数组中删除值x的所有元素

let arr = ['a', 'b', 'c', 'b']

How can I remove all elements of value 'b' from arr?

如何从arr中删除所有值“b”的元素?

6 个解决方案

#1


86  

A filter:

一个过滤器:

 let farray = arr.filter {$0 != "b"} 

#2


9  

var array : [String]
array = ["one","two","one"]

let itemToRemove = "one"

while array.contains(itemToRemove) {
    if let itemToRemoveIndex = array.index(of: itemToRemove) {
        array.remove(at: itemToRemoveIndex)
    }
}

print(array)

Works on Swift 3.0.

3.0工作迅速。

#3


2  

EDITED according to comments:

编辑根据评论:

I like this approach:

我喜欢这个方法:

var arr = ["a", "b", "c", "b"]

while let idx = arr.index(of:"b") {
    arr.remove(at: idx)
}

Original answer (before editing):

最初的回答(编辑之前):

let arr = ['a', 'b', 'c', 'b']

if let idx = arr.index(of:"b") {
    arr.remove(at: idx)
}

#4


1  

A general approach is to exploit first class procedures. (However, this approach is much more powerful than what is required for your question.) To illustrate, say you want to avoid "Justin" repeatedly in many collections.

一种通用的方法是利用一流的程序。(不过,这种方法比您的问题所要求的要强大得多。)为了举例说明,假设您希望在许多集合中避免重复使用“Justin”。

let avoidJustin = notEqualTester ("Justin")

let arrayOfUsers = // ...

arrayOfUsers.filter (avoidJustin)

let arrayOfFriends = // ...

arrayOfFriends.filter (avoidJustin)

With this, you avoid repeatedly creating a closure each time you want to avoid Justin. Here is notEqualTester which, given a that, returns a function of this that returns this != that.

这样,您就可以避免在每次要避免Justin时重复创建一个闭包。这里是notEqualTester,给定一个that,它返回一个函数返回这个!=那个。

func notEqualTester<T: Equatable> (that:T) -> ((this:T) -> Bool) {
  return { (this:T) -> Bool in return this != that }
}

The returned closure for this captures the value for that - which can be useful when that is no longer available.

返回的闭包捕获了该闭包的值——当该值不再可用时,这个值是有用的。

#5


1  

In Swift 3 I simply do:

在Swift 3中,我只需要:

arr = arr.filter { $0 != "a" } 

.filter, .sort and .map are great for saving time and solve lots of problems with little code.

.filter, .sort和.map对于节省时间和用很少的代码解决很多问题都很有用。

This article has good examples and explain the differences and how they work: https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/

本文提供了一些很好的示例,并解释了它们的区别和工作原理:https://useyourloaf.com/blog/swift-guide to map-filer -reduce/

#6


0  

If you need to modify initial array, you can use the function removeAll(where:) that is available in Xcode 10:

如果需要修改初始数组,可以使用Xcode 10中可用的函数removeAll(where:):

var arr = ["a", "b", "c", "b"]
arr.removeAll(where: { $0 == "b" })
print(arr) // output is ["a", "c"]

However if you are using Xcode 9 you can find this function in Xcode9to10Preparation (this library provides implementations of some new functions from Xcode 10).

但是,如果您正在使用Xcode9,您可以在xcode9to10prepare中找到这个函数(这个库提供Xcode 10中的一些新函数的实现)。

#1


86  

A filter:

一个过滤器:

 let farray = arr.filter {$0 != "b"} 

#2


9  

var array : [String]
array = ["one","two","one"]

let itemToRemove = "one"

while array.contains(itemToRemove) {
    if let itemToRemoveIndex = array.index(of: itemToRemove) {
        array.remove(at: itemToRemoveIndex)
    }
}

print(array)

Works on Swift 3.0.

3.0工作迅速。

#3


2  

EDITED according to comments:

编辑根据评论:

I like this approach:

我喜欢这个方法:

var arr = ["a", "b", "c", "b"]

while let idx = arr.index(of:"b") {
    arr.remove(at: idx)
}

Original answer (before editing):

最初的回答(编辑之前):

let arr = ['a', 'b', 'c', 'b']

if let idx = arr.index(of:"b") {
    arr.remove(at: idx)
}

#4


1  

A general approach is to exploit first class procedures. (However, this approach is much more powerful than what is required for your question.) To illustrate, say you want to avoid "Justin" repeatedly in many collections.

一种通用的方法是利用一流的程序。(不过,这种方法比您的问题所要求的要强大得多。)为了举例说明,假设您希望在许多集合中避免重复使用“Justin”。

let avoidJustin = notEqualTester ("Justin")

let arrayOfUsers = // ...

arrayOfUsers.filter (avoidJustin)

let arrayOfFriends = // ...

arrayOfFriends.filter (avoidJustin)

With this, you avoid repeatedly creating a closure each time you want to avoid Justin. Here is notEqualTester which, given a that, returns a function of this that returns this != that.

这样,您就可以避免在每次要避免Justin时重复创建一个闭包。这里是notEqualTester,给定一个that,它返回一个函数返回这个!=那个。

func notEqualTester<T: Equatable> (that:T) -> ((this:T) -> Bool) {
  return { (this:T) -> Bool in return this != that }
}

The returned closure for this captures the value for that - which can be useful when that is no longer available.

返回的闭包捕获了该闭包的值——当该值不再可用时,这个值是有用的。

#5


1  

In Swift 3 I simply do:

在Swift 3中,我只需要:

arr = arr.filter { $0 != "a" } 

.filter, .sort and .map are great for saving time and solve lots of problems with little code.

.filter, .sort和.map对于节省时间和用很少的代码解决很多问题都很有用。

This article has good examples and explain the differences and how they work: https://useyourloaf.com/blog/swift-guide-to-map-filter-reduce/

本文提供了一些很好的示例,并解释了它们的区别和工作原理:https://useyourloaf.com/blog/swift-guide to map-filer -reduce/

#6


0  

If you need to modify initial array, you can use the function removeAll(where:) that is available in Xcode 10:

如果需要修改初始数组,可以使用Xcode 10中可用的函数removeAll(where:):

var arr = ["a", "b", "c", "b"]
arr.removeAll(where: { $0 == "b" })
print(arr) // output is ["a", "c"]

However if you are using Xcode 9 you can find this function in Xcode9to10Preparation (this library provides implementations of some new functions from Xcode 10).

但是,如果您正在使用Xcode9,您可以在xcode9to10prepare中找到这个函数(这个库提供Xcode 10中的一些新函数的实现)。