Skip to content

Instantly share code, notes, and snippets.

@JoshuaSullivan
Last active June 27, 2019 16:19
Show Gist options
  • Save JoshuaSullivan/72cfabb9ac63aa2721d5 to your computer and use it in GitHub Desktop.
Save JoshuaSullivan/72cfabb9ac63aa2721d5 to your computer and use it in GitHub Desktop.
Don't mourn the removal of --, ++ and the C-style for loop from Swift. Read the blog post: http://www.chibicode.org/?p=24
let baseString = "/Documents/"
let words = ["Alpha", "Beta", "Gamma", "Delta"]
var paths : [String] = []
for (var i = 0; i < words.count; ++i) {
let word = words[i]
paths.append("\(baseString)\(word)")
}
print(paths)
for index, word in words.enumerate() {
funcThatRequiresWordAndIndex(word, index: index)
}
let n = 4
print(--n) // Prints '3', value of n is now 3.
print(++n) // Prints '4', value of n is now 4.
print(n--) // Prints '4', value of n is now 3. Confusing!
print(n++) // Prints '3', value of n is now 4. Also confusing!
for i in 0..<words.count {
funcThatRequiresWordAndIndex(words[i], index: i)
}
let baseString = "/Documents/"
let words = ["Alpha", "Beta", "Gamma", "Delta"]
let paths = words.map({"\(baseString)\($0)"})
print(paths)
@SixFiveSoftware
Copy link

Line 1 of IncrementDecrementExample.swift should be var if you're mutating it later in the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment