I have a class that looks like this:
class A {
var aString = ""
static var staticString = ""
}
If I create an instance of A I can't access the static property:
var a = A()
a.staticString = "THIS GIVES AN ERROR"
However, if I create a direct instance to the static variable it works:
var a = A.staticString
a = "THIS WORKS"
The way I understand static variables is that you should only be able to access them directly like this: A.staticString = "hello". But this doesn't seem to be the case.
What's more confusing (to me) is that I can create multiple instances with their own seperate values; that is the value doesn't remain static:
var a = A.staticString
a = "AAA"
var b = A.staticString
b = "BBB"
print(a) //prints AAA
print(b) //prints BBB
Isn't the whole point that a static variable should... remain static? In my head, both a and b should print BBB since b = "BBB" should have overwritten the first value assigned to it.
To make it even more confusing (to me), using a singleton does give me the result I expect:
class A {
static let shared = A()
var aString = ""
static var staticString = ""
}
let instance1 = A.shared
instance1.aString = "A String"
let instance2 = A.shared
instance2.aString = "Another String"
print(instance1.aString, instance2.aString) //Both print "Another String"
Could some kind soul try to clear things up for me?
aandb, they have nothing to do with the static variable, just two string varaibles.