4
3

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 5 years have passed since last update.

Swift:省略記法を使わないflatMap,compactMap

Posted at
flatMap
let array: [[String]] = [["apple", "banana"], ["grape", "peach"]]

let flatArray = array.flatMap { (innerArray) -> [String] in
    return innerArray
}

Swift.print(flatArray)
// -> ["apple", "banana", "grape", "peach"]
compactMap
let array: [String?] = ["apple", "banana", nil, "grape"]

let nonNilArray = array.compactMap { (str) -> String? in
    return str
}

Swift.print(nonNilArray)
// -> ["apple", "banana", "grape"]

私は$0を使う省略記法あまり好きではないです...

4
3
1

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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?