Created
October 16, 2020 03:13
-
-
Save marslin1220/8d6d01aafe317e661ab7bfec6aa72bbd to your computer and use it in GitHub Desktop.
Mutation with Struct & Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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