5

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

}
Confused
  • 6,048
  • 6
  • 34
  • 75
  • You should update your question with some relevant code. Have you done any tests to see what results you get? – rmaddy Nov 20 '16 at 23:57
  • @rmaddy I think he / she is not quite sure all of the terminology in what asking :) Confused, See my answer for some clarity (I hope!) – Fluidity Nov 21 '16 at 00:25
  • No, @maddy, have not run any tests. have updated question to show what I'm hoping happens. Don't know how to make it so. – Confused Nov 21 '16 at 00:51
  • @maddy, quick question... once a `bool` is wrapped in a class, and referenced as being a property of that class, is that a reference, even though it's a boolean (which is a struct/value type underneath) because it's become a reference to the class first, and the property second? – Confused Nov 21 '16 at 01:41
  • @maddy, I'm running something as per the included code in the question, and it doesn't work... the value isn't changing in the static var, only locally in iAmOn, inside the Button() instance. – Confused Nov 22 '16 at 08:23

1 Answers1

5

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`
Fluidity
  • 3,985
  • 1
  • 13
  • 34
  • @AlexanderMomchilov thanks alex I was still working on formatting once I realized that I missed the point of the Q and was trying to update info :) – Fluidity Nov 21 '16 at 00:21
  • 2
    Just to clarify, there is no `NSUnsafeMutablePointer` Objective-C type. `UnsafeMutablePointer` is a pure Swift type used when C pointers are bridged to Swift. But they can also be used in pure Swift as well (`alloc`/`free`, etc.), but you really need a good reason to get that low level. – JAL Nov 21 '16 at 00:42
  • @JAL you know I thought it was just `UnsafeMutablePointer` a regular type but then I just threw the NS on there on a whim. I should have checked first.. I will update. Thanks!! – Fluidity Nov 21 '16 at 00:43
  • @Confused, see my first code block, the `z2` example. You want `iAmOn` VALUE to be a REFERENCE to `MenuScene.myButtonState`. So, `var iAmOn = MenuScene.myButtonState` THen you will get what you want as I showed in my block – Fluidity Nov 21 '16 at 00:59
  • how can a `let` assigned to the state (boolean) be changeable? – Confused Nov 21 '16 at 01:03
  • @Confused, I seem to be in contact with you frequently. Would you like to chat for a few min somewhere? – Fluidity Nov 21 '16 at 01:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128592/discussion-between-confused-and-fluidity). – Confused Nov 21 '16 at 01:05
  • @Confused I'm in http://chat.stackoverflow.com/rooms/127579/swift if you want I can tell you a few things – Fluidity Nov 21 '16 at 01:05
  • In your second example, z2, and as per your comment yesterday, I have passed a reference to this static var as a parameter into a function that creates a new object based on this state, and then creates an internal reference to this passed in reference to the static var. But changing the value of this reference to true/false does not change the original static var's value. Is there something I'm missing? – Confused Nov 22 '16 at 08:19
  • @Confused use `inout` in your parameter which should do the trick – Fluidity Nov 22 '16 at 20:45
  • As soon as someBool.val is defined as having a type of Bool, in the parameter list of the function declaration and then used in the definition, it's a value, not the pointer to the someBool.val container for the value. – Confused Nov 22 '16 at 21:09
  • @Confused I'm honestly not a swift guru by any means (ask Martin R or someone) but functions try to copy (value) whenever possible (in my limited understanding). So that is why your "reference" wasn't working without `inout`, because Swift doesn't have a strict pointer / reference system (that I'm aware of) outside of the ObjectiveC types – Fluidity Nov 23 '16 at 00:18
  • @Confused I can say that if you put an object of a class like `SKNode` as a parameter, you *will* get a reference even without `inout`... – Fluidity Nov 23 '16 at 00:20
  • Yes, I know. Anything that's a class type gets passed as a class type, because that's stipulated, and because it's a reference. But these simple little booleans are deceptively tricky to pass references off: for me. I've had no luck with any of these techniques. Everything is passing the value, not the pointer/reference. – Confused Nov 23 '16 at 06:10
  • If you need a chuckle, have a look at my convoluted attempts to get the activity to work as I want, but not in the way I'd like... http://stackoverflow.com/questions/40745099/how-to-pass-a-reference-to-a-boolean-rather-than-its-value – Confused Nov 23 '16 at 06:11