I found this in some open source code.
I am familiar with map(), but not the CGFloat.init(_:))
part.
The Swift code is as follows:
if let array = propertyListValue as? [Int], array.count == 3 || array.count == 4 {
let floats = array.map(CGFloat.init(_:))
}
Not sure what to look for in docs. I know what _ name : Type
means in a function declaration... but just confused by this use case.
CodePudding user response:
map
takes one argument, a function that accepts an argument of the sequence element type and returns any type. CGFloat.init
is the name of (technically a reference to) just such a function.
Perhaps you think of map
as taking a "closure", and indeed you could equivalently have said
map { CGFloat($0) }
here; but that's in fact exactly the same sort of function — it's just that instead of the function's name (reference) you're supplying its body.