I'm trying to understand some of the short hand syntax used by the map function.
The following is the setup
let array = [1, 2, 3]
// these make sense
let arr1 = array.map({String($0)})
let arr2 = array.map{String($0)}
let arr3 = array.map({ number in
return String(number)
})
let arr4 = array.map({ (number) -> String in
String(number)
})
Here is where the confusion lays. In swift I can forgo the curly braces for map, but this seems like something that can't be done, for my own functions where I have a trailing closure. Some magical inference that's being made perhaps? Also why is the String initialized in this way?
// this doesn't make sense. Foregoing the curly braces? I can't do that!!!
let arr5 = array.map(String.init)
let arr6 = array.map(String()) // Compile Error: Cannot convert value of type 'String' to expected argument type '@noescape (Int) throws -> _'
This is me trying to use similar syntax as map
func crap(block:(Int)-> String) {
print("Int to string block" + block(1));
}
// works obviously
crap{ "\($0) some garbage" }
// compile error : Anonymous closure argument not contained in a closure
crap( "\($0) some garbage" )