Created
September 28, 2017 22:01
-
-
Save adamyanalunas/982a24f7433f13e8ad288e38a64f1ef7 to your computer and use it in GitHub Desktop.
Swift 4 array of string iteration differences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Normal array of strings | |
let array = ["This", "is", "string", "array"] | |
let characters = array.flatMap { | |
$0.characters | |
} | |
// This prints ["T", "h", "i", "s", "i", "s", "s", "t", "r", "i", "n", "g", "a", "r", "r", "a", "y"] | |
print(characters) | |
let words = array.flatMap { | |
return $0 | |
print($0) | |
} | |
// This prints: ["This", "is", "string", "array"] | |
print(words) | |
let typedReturn = array.flatMap { string -> String in | |
return string | |
print(string) | |
} | |
// This prints: ["T", "h", "i", "s", "i", "s", "s", "t", "r", "i", "n", "g", "a", "r", "r", "a", "y"] | |
print(typedReturn) | |
let guess = array.flatMap { $0 } | |
// This prints: ["T", "h", "i", "s", "i", "s", "s", "t", "r", "i", "n", "g", "a", "r", "r", "a", "y"] | |
print(guess) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment