Summary:
The answers above were true up until Beta 3 (and may change again in future releases)
Slice now acts just like an array, but as @matt said above, is effectively a shallow copy to an array under the hood, until a change is made. Slices (now) see a snapshot of the original values,
Also note that slice syntax has changed:
[from..upToButNotIncluding] -> [from..<upToButNotIncluding]
Example:
var arr = ["hello", "world", "goodbye"] // ["hello", "world", "goodbye"]
var arrCopy = arr
let slice = arr[0..<2] // ["hello", "world"]
arr[0] = "bonjour"
arr // ["bonjour", "world", "goodbye"]
arrCopy // ["hello", "world", "goodbye"]
slice // ["hello", "world"]
This allows much more uniform processing, as it is simpler (IMHO) to do python style list processing - filtering one list to make another. per Matt's answer prior to Beta 3, you had to create a temporary array in order to map a slice. The new code is now simpler:
class NameNumber {
var name:String = ""
var number:Int = 0
init (name:String, number:Int) {
self.name = name
self.number = number
}
}
var number = 1
let names = ["Alan", "Bob", "Cory", "David"]
let foo = names[0..<2].map { n in NameNumber(name:n, number:number++) }
foo // [{name "Alan" number 1}, {name "Bob" number 2}]
(though to be fair, foo is still a slice)
Reference:
http://adcdownload.apple.com//Developer_Tools/xcode_6_beta_3_lpw27r/xcode_6_beta_3_release_notes__.pdf
Important Changes, Issues Resolved,
- Swift Language, Paragraph 1
"Array in Swift has been completely redesigned to have full value semantics like Dictionary and
String...m"