![見出し画像](https://assets.st-note.com/production/uploads/images/6701148/rectangle_large_e8de5aa276b6c01ff0bd6590cb000729.jpg?width=800)
Photo by
dngri
Swiftでいこう -- flatMap?
flatMapでなにができるかというと、
let a = ["a", "b", nil].flatMap{ $0 }
print(a)
let b = [["a"], ["b"], ["c", "d"]].flatMap{ $0 }
print(b)
["a", "b"]
["a", "b", "c", "d"]
と表示されます。nilをはずしたり、配列をくっつけたりします。
しかし、Swift4.1では
let a = ["a", "b", nil].flatMap{ $0 }
を実行すると、
'flatMap' is deprecated: Please use compactMap(_:) for the case where closure returns an optional value
と出るて"compactMapに入れ替えなさい"ということみたい。
この記事が気に入ったらサポートをしてみませんか?