0
1

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 FilterとMapを使う

Last updated at Posted at 2020-11-05

巨大データを扱う時に、例えば写真のデータ:
id,filepath,date,filenameなどなどの基本情報
isSelected,rating,openEyeなどなどのアプリに必要なフラグ

こんなものがありますが、必要におうじて様々な配列を抽出する必要がしばしば

FilterとMapを使います。

ImageViewModel
struct ImageViewModel {
    let id: String = ""
    let name: String = ""
    var isSelected: Bool = false
}

isSelectがtrueになっているすべての写真を取得して、
それらの写真のidを新しい配列として出したい時:

let imageViewModels = [ImageViewModel]()
let targetImageIds = imageViewModels.filter({ $0.isSelected }).map({ $0.id })
0
1
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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?