Skip to content

Instantly share code, notes, and snippets.

@cSquirrel
Last active August 29, 2015 14:17
Show Gist options
  • Save cSquirrel/0ad63991ce3997ea4977 to your computer and use it in GitHub Desktop.
Save cSquirrel/0ad63991ce3997ea4977 to your computer and use it in GitHub Desktop.
class MyClass : NSObject {
var anArray:Array<String>?
func printDebug() -> String {
if (anArray != nil) {
return "Array has \(anArray!.count) element(s)"
} else {
return "No array"
}
}
func setArray(inout otherArray:Array<String>) {
// copy happens here
self.anArray = otherArray
}
}
let mc = MyClass()
mc.printDebug()
var items = Array<String>()
items.append("Item 1")
var otherItems = items
mc.anArray = items
mc.printDebug() // "Array has 1 element(s)"
items.append("Item 2") // ["Item 1", "Item 2"]
// since mc's array is a copy it's out of sync
mc.printDebug() // "Array has 1 element(s)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment