Answers for "swift array map"

0

map swift

// A map example which maps each item in  this array of numbers
// to its value multiplied by 2.

let array = [1, 2, 3, 4]

// Full closure
print(array.map({ (num: Int) -> Int in return num * 2 }))

// Abbreviated trailing closure, with inferred arguments, 
// argument type, and return type.
print(array.map { $0 * 2 })

// Output: [2, 4, 6, 8]
Posted by: Guest on December-23-2020
0

swift array map example

swift 5
// taken from developer.apple.com map(_:)
let cast = ["Vivien", "Marlon", "Kim", "Karl"]
let lowercaseNames = cast.map { $0.lowercased() }
// 'lowercaseNames' == ["vivien", "marlon", "kim", "karl"]
let letterCounts = cast.map { $0.count }
// 'letterCounts' == [6, 6, 3, 4]
Posted by: Guest on September-29-2021

Code answers related to "Swift"

Browse Popular Code Answers by Language