Confused Confused - 11 days ago 5
Swift Question

Is a static boolean a reference type in Swift?

I'm making some switches. In my MenuScene class there's some booleans that are static variables, booleans, to represent the states of these switches.

Are these addressable as reference types, so I can be sure that other objects are able to change their state with a unique reference to them?

The dream, in my dreamy pseudo code, I'm hoping changes to

iAmOn
impact the state of
myButtonABC_state


class MenuScene {
static var myButtonABC_state: Bool = false
static var myButtonXYZ_state: Bool = false

override onDidMoveToView {

let buttonABC = Button(withState: MenuScene.myButtonABC_state)
let buttonXYZ = Button(withState: MenuScene.myButtonXYZ_state)
}
}


In a button class

class Button {

var iAmOn: Bool = false

init(withState state: Bool){
iAmOn = state
}

override onTouchesBegun(... etc...){
if iAmOn { iAMOn = false }
else { iAmOn = true}
}

}

Answer

Bool is a struct in Swift; structs are value types. It doesn't matter if it's static var, class var, let, var, etc., the type is what matters--so no, Bool is value type.

I think you are not 100% on all of the terminology (mostly because Apple doesn't really cover it much in documentation as usual, lol).

There are "Swift Types" (Bool, Int, your classes/structs, etc), and "Variable/Constant Types" (which hold data in a memory register, such as references or actual-values), as well as "Memory Register Write/Read Types" (variable vs vonstant, mutable vs immutable, var vs let).

Don't be frustrated.. It's a bit confusing for everyone... Especially at first and without great documentation. (I tried learning C++ pointers early age and it was way over my head).

Here's a good reference material: (towards the bottom) https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ClassesAndStructures.html

Basically, if you want to hold a reference to something, you have to use a Reference Type memory register. This means using a class instance Static makes no difference:

/* Test1: */

struct Hi {
    static var sup = "hey"
}

var z = Hi.sup
Hi.sup = "yo"

print(z) // prints "hey"

/* Test 2: */

class Hi2 {
    static var sup = "hey"
}

var z2 = Hi2.sup
Hi2.sup = "yo"

print(z2) // Prints "hey"

If you are using static inside of a class, then it will be a reference type and do as you are wanting. If you put it inside of a struct it will not

If you feel like you need a pointer to something that isn't inside of a class, then you can use UnsafeMutablePointer or something like that from OBJc code.

Or, you can wrap a bool inside of a class object (which are always references).

final class RefBool {
   var val: Bool
   init(_ value: Bool) { val = value }
}

And here is some interesting behavior for reference types using let:

let someBool: RefBool

someBool = RefBool(true)
someBool = RefBool(false) // wont compile.. someBool is a `let`
someBool.val = false      // will compile because of reference type and member is `var`
Comments