0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[Swift]配列から要素を削除する方法まとめ

Last updated at Posted at 2020-10-16

何回も検索をかけてしまうのでまとめておく。

最初の要素削除

var strArray = ["A","B","C","D","E"]

strArray.removeFirst()  //["B", "C", "D", "E"]

最後の要素削除

var strArray = ["A","B","C","D","E"]

strArray.removeLast()  //["A", "B", "C", "D"]

全ての要素削除

var strArray = ["A","B","C","D","E"]

strArray.removeAll()  //[]

指定した要素を削除

var strArray = ["A","B","C","D","E"]

strArray.removeAll(where: {$0 == "C"}) // ["A", "B", "D", "E"]

インデックスを指定して要素削除

var strArray = ["A","B","C","D","E"]

strArray.remove(at: 2) //["A", "C", "D", "E"]

インデックスが不明な要素を削除 (これまでのが使えない時)

"C"を削除したい場合、"C"以外をfilterで指定する。

var strArray = ["A","B","C","D","E"]

strArray = strArray.filter { $0 != "C" } // ["A", "B", "D", "E"]

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?