Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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} 
    }

}
like image 951
Confused Avatar asked Nov 20 '16 23:11

Confused


People also ask

Which are reference types in Swift?

In Swift, reference type instances share a single copy of their data, so that every new instance will point to the same address in memory. A typical example is a class , function , or closure . This function prints the address of an object, which will help you check whether you're referencing the same instance or not.

Can a boolean be static?

Fields: static Boolean FALSE : The Boolean object corresponding to the primitive value false. static Boolean TRUE : The Boolean object corresponding to the primitive value true. static Class : The Class object representing the primitive type boolean.

What is a static variable in Swift?

Static variables are those variables whose values are shared among all the instance or object of a class. When we define any variable as static, it gets attached to a class rather than an object. The memory for the static variable will be allocation during the class loading time.

Are strings reference types Swift?

In Objc string, array and dictionary are all reference types, while in Swift they are all value types.


1 Answers

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 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`
like image 109
Fluidity Avatar answered Sep 25 '22 20:09

Fluidity