Skip to content

Instantly share code, notes, and snippets.

@marslin1220
Created October 16, 2020 03:13
Show Gist options
  • Save marslin1220/8d6d01aafe317e661ab7bfec6aa72bbd to your computer and use it in GitHub Desktop.
Save marslin1220/8d6d01aafe317e661ab7bfec6aa72bbd to your computer and use it in GitHub Desktop.
Mutation with Struct & Class
struct AStruct {
let letRefValue = "foo"
var varRefValue = "bar"
let letDataValue = 0
var varDataValue = 0
func changeLetRefValue() {
letRefValue = "hello" //< Cannot assign to property: 'letRefValue' is a 'let' constant
}
func changeVarRefValue() {
varRefValue = "hello" //< Cannot assign to property: 'self' is immutable
}
func changeLetDataValue() {
letDataValue = 1 //< Cannot assign to property: 'letDataValue' is a 'let' constant
}
func changeVarDataValue() {
varDataValue = 1 //< Cannot assign to property: 'self' is immutable
}
}
class AClass {
let letRefValue = "foo"
var varRefValue = "bar"
let letDataValue = 0
var varDataValue = 0
func changeLetRefValue() {
letRefValue = "hello" //< Cannot assign to property: 'letRefValue' is a 'let' constant
}
func changeVarRefValue() {
varRefValue = "hello"
}
func changeLetDataValue() {
letDataValue = 1 //< Cannot assign to property: 'letDataValue' is a 'let' constant
}
func changeVarDataValue() {
varDataValue = 1
}
}
let letAStruct = AStruct()
letAStruct.letRefValue = "hello" //< Cannot assign to property: 'letRefValue' is a 'let' constant
letAStruct.varRefValue = "hello" //< Cannot assign to property: 'letAStruct' is a 'let' constant
letAStruct.letDataValue = 1 //< Cannot assign to property: 'letDataValue' is a 'let' constant
letAStruct.varDataValue = 1 //< Cannot assign to property: 'letAStruct' is a 'let' constant
var varAStruct = AStruct()
varAStruct.letRefValue = "hello" //< Cannot assign to property: 'letRefValue' is a 'let' constant
varAStruct.varRefValue = "hello"
varAStruct.letDataValue = 1 //< Cannot assign to property: 'letDataValue' is a 'let' constant
varAStruct.varDataValue = 1
let letAClass = AClass()
letAClass.letRefValue = "hello" //< Cannot assign to property: 'letRefValue' is a 'let' constant
letAClass.varRefValue = "hello"
letAClass.letDataValue = 1 //< Cannot assign to property: 'letDataValue' is a 'let' constant
letAClass.varDataValue = 1
var varAClass = AClass()
varAClass.letRefValue = "hello" //< Cannot assign to property: 'letRefValue' is a 'let' constant
varAClass.varRefValue = "hello"
varAClass.letDataValue = 1 //< Cannot assign to property: 'letDataValue' is a 'let' constant
varAClass.varDataValue = 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment