lucasgv
6/14/2018 - 2:19 PM

Swift Concepts - (Struct/Classes)

Structs: Value Types

Classes: Reference Types

// Using a Class
class House {
    var doorIsOpen: Bool
}
var myHouse = House(doorIsOpen: true)
var yourHouse = House(doorIsOpen: false)
print(myHouse.doorIsOpen)    //prints true
// assign yourHouse equal to myHouse
yourHouse = myHouse
// Close the door on your house
yourHouse.doorIsOpen = false
// Recheck the value of myHouse's door
print(myHouse.doorIsOpen)    // prints false

// Using a struct
struct HouseStruct {
    var doorIsOpen: Bool
}
var myHouseStruct = HouseStruct(doorIsOpen: true)
var yourHouseStruct = HouseStruct(doorIsOpen: false)
print(myHouseStruct.doorIsOpen)     // prints true
yourHouseStruct = myHouseStruct
yourHouseStruct.doorIsOpen = false
print(myHouseStruct.doorIsOpen)   // prints true