Skip to content

Instantly share code, notes, and snippets.

@andymuncey
Last active October 5, 2020 16:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andymuncey/f6fe94a3f952f3d0db40f118a6bbcc1f to your computer and use it in GitHub Desktop.
Save andymuncey/f6fe94a3f952f3d0db40f118a6bbcc1f to your computer and use it in GitHub Desktop.
Demonstrates pass by reference vs pass by value difference with Swift struct and class
//change this to a struct and verify the change in behaviour
class Number : CustomStringConvertible {
var number: Int
init(_ num: Int) {
number = num
}
public var description : String {
return "\(number)"
}
//used to get description of type
var type : String {
return String(describing: Mirror(reflecting: self).displayStyle!)
}
}
var intA = Number(2)
var intB = intA //passing value by reference(class) or value(struct)
intA.number = 7 //reassign value of intA (intB will only be reassigned if passed by reference)
print("Working with \(intA.type)")
print("intA value: \(intA)")
print("intB value: \(intB)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment