How can I access an array in one of my classes?

myGV is a structure where I store a handful of global variables.

The following are all sub-classes of SKSpriteNode: I have a class called guessingBar which holds 6 guessSlots. The former class has an array of the guessSlots for me to loop through.

I just don't know the syntax of how to access the array.

myGV holds multiple variables, so the SKSpriteNode guessBar can be found at:

myGV.guessBar

I expected to be able to read the array with:
myGV.guessBar.guessSlots[x] but as you can see from the debugger screenshot, I cannot.

In the screenshot you can see that everything is initialized. Am I missing some silly typo, or is the syntax escaping me?

http: //98.7.37.117/s.png


Answered by SergioDCQ in 676976022

All my fault. It's right there in the debugger too. My SKSpriteNode should be declared as the subclass, not an SKSpriteNode. That's why I can't access the array. The array exists in my subclass.

I cannot access the screenshot.
It would be much better to show the code itself.

I have a class called guessingBar

guessingBar is the class of guessBar ?
If so, you should name it starting with uppercase: GuessingBar

guessingBar which holds 6 guessSlots

So guessSlots is an Array, of what ?
Or do you have 6 different guessSlots vars (guessSlots1, guessSlots2…)

myGV is a structure

what is the name of the struct ?
Should be MyGV

myGV is an instance of the struct ? Exact ?
var myGV : MyGV
How did you initialise it ?

So, there is too much guessing here, please make it clear.

I expected to be able to read the array

Tell what error you get

I cannot access the screenshot.

I wish I knew what was up with my server, sorry

myGV is a structure to hold a few global variables.

myGV.guessBar = GuessBar : SKSpriteNode

The GuessBar has 6 children, GuessSlot : SKSpriteNode. GuessBar has an array that has all six instances of them.

Here is the code for GuessBar:

class GuessBar : SKSpriteNode{
    var guessSlots : [GuessSlot] = []

    init(color: SKColor, size: CGSize) {
       super.init(texture: nil, color: color, size: size)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 }

Here is the code where I fill the arrays in the guessBar

func createGuessBar(color: SKColor, size: CGSize, factor: Int) -> GuessBar
{
    let myGuessBar = GuessBar(color : color, size: size)

    //other unrelated code

    for x in 0...5{

        let mGuessSlot = GuessSlot(color: .clear, size: CGSize(width: iFactor, height: rFactor), width: myGuessBar.size.width,  iFactor: x, guessBarRef: myGuessBar)
        myGuessBar.guessSlots.append(mGuessSlot)
    }
    //other unrelated code

    return myGuessBar

}

So the exact info from the debugger reads:

myGV.guessBar = (SKSpriteNode?) 0x0000600001984580

*SpriteKit.SKSpriteNode (SKSpriteNode)

*guessSlots ([GuessAgain.GuessSlot]) 6 values

myGV.guessBar.guessSlots = Invalid Expression

The way I see it, everything is running fine. If I expand the guessSlots in the debugger it lists each value. I just can't get the syntax right, going by the last line in the debugger, to access that array from my main structure myGV.

Accepted Answer

All my fault. It's right there in the debugger too. My SKSpriteNode should be declared as the subclass, not an SKSpriteNode. That's why I can't access the array. The array exists in my subclass.

How can I access an array in one of my classes?
 
 
Q