Skip to content

Instantly share code, notes, and snippets.

@coreyd303
Last active October 2, 2020 18:52
Show Gist options
  • Save coreyd303/7b62cb980f423dbd62be00e331c692b5 to your computer and use it in GitHub Desktop.
Save coreyd303/7b62cb980f423dbd62be00e331c692b5 to your computer and use it in GitHub Desktop.
This is a code sample demonstrating the basics of Structs in Swift
struct Car {
let make: String
let model: String
var color: String
private(set) var mileage: Int? = 0
mutating func update(mileage: Int) {
self.mileage = mileage
}
func tryUpdate(mileage: Int) {
self.mileage = mileage // not allowed, because this func lacks the mutating keyword
}
func nextYearsMileage(adding: Int) -> Int {
return self.mileage! + adding // allowed because this func does not mutate the value of mileage
}
enum Drive {
case forward
case backward
var go: String {
switch self {
case .forward:
return "Vroom Vroom"
case .backward:
return "Beep Beep Beep"
}
}
}
class Seller {
var name: String
}
struct Tires {
var size: Int
}
}
// Stucts provide a default initializer, and can be intialized at mutable or imutable instances
var myTruck = Car(make: "GMC", model: "Sierra", color: "Black")
// if the struc is a variable, public properties can be mutated via direct assignment
print(myTruck.color) // -> Black
myTruck.color = "Blue"
print(myTruck.color) // -> Blue
// private setters can be mutated via mutating functions
print(myTruck.mileage!) // -> 0
myTruck.update(mileage: 100)
print(myTruck.mileage!) // -> 100
// non-mutating functions cannot chance values of struct properties
myTruck.tryUpdate(mileage: 200) // -> Error
//error: Structs.playground:15:14: error: cannot assign to property: 'self' is immutable
//self.mileage = mileage
//~~~~ ^
//
//Structs.playground:14:5: note: mark method 'mutating' to make 'self' mutable
//func tryUpdate(mileage: Int) {
// ^
// mutating
// initializing a constant make the object imutable
let workTruck = Car(make: "Ford", model: "f150", color: "White")
workTruck.update(mileage: 100)
print(workTruck.mileage!) // -> Error
//error: Structs.playground:46:11: error: cannot use mutating member on immutable value: 'workTruck' is a 'let' constant
//workTruck.update(mileage: 100)
//~~~~~~~~~ ^
//
//Structs.playground:45:1: note: change 'let' to 'var' to make it mutable
//let workTruck = Car(make: "Ford", model: "f150", color: "White")
//^~~
//var
// creating a new instance of the workTruck creates a separate instance, so mutations do not impact the original instance
var newTruck = workTruck
newTruck.update(mileage: 123)
print(newTruck.mileage!) // -> 123
print(workTruck.mileage!) // -> 0
// non-mutating funcs are allowed if they don't change internal values
print(newTruck.nextYearsMileage(adding: 100)) // -> 223
// Enums are allowed, however they are maintained at the Static level unless stored in an internal variable
print(Car.Drive.forward.go) // -> Vroom Vroom
print(Car.Drive.backward.go) // -> Beep Beep Beep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment