Skip to content

Instantly share code, notes, and snippets.

@yossan
Last active October 4, 2017 15:09
Show Gist options
  • Select an option

  • Save yossan/72e612be59f8b51229cc3eee802f102d to your computer and use it in GitHub Desktop.

Select an option

Save yossan/72e612be59f8b51229cc3eee802f102d to your computer and use it in GitHub Desktop.
[Swift4] String's flatMap
var message: String = "test"
// Returns Array<Character>
let conv = message.flatMap { (chr) /*Character*/ in
return String(chr) + "/"
}
print(type(of: conv)) //Array<Character>
print(conv) //["t", "/", "e", "/", "s", "/", "t", "/"]
//https://developer.apple.com/documentation/swift/string/2903352-flatmap
let conv2 = message.flatMap { (chr) /*Character*/ -> String? in
return String(chr) + "/"
}
print(type(of: conv2)) //Array<String>
print(conv2) //["t/", "e/", "s/", "t/"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment