Why do Let Integer constants change values or always equal Zero?

I have about 15 global Integer constants in my app. Half of them display zero.

For example...


struct Global {
    struct lives {
        static let free = 3
        static let paid = 5
    }
}
let CRATE_LOCATION_ZERO = CGPoint(x: -10000, y: -10000)

Then later in code I try to use them...

let purchasedApp = true
let newLives = purchasedApp ? paid : free
let newCrateLocation = CRATE_LOCATION_ZERO

When I run the program and get these values, they are always zero. When I add a break point and check the values, they are also zero.

  • The newLives will be 0
  • The CGPoint will be x:0, y:0

But not all the static (or constant) values end up being 0. Then some others have different values.

I've also put up a video to show you what I mean.

leckett.net/video/wierd.mov

CPU and memory are all okay. I'm running Xcode 12.5.1

Has anybody experienced this?

static let variables (or you want to call them constants) are initialized dynamically on the first use. Did you check them in the debugger after all of them are initialized?

Left blank

Yes. The ones I showed in the video were all initialized.

How I discovered the problem is that doing an IAP started resulting in no longer getting new lives (the code basically validates the receipt and then adds the (static let variable) to a numberOfLives var variable.

It seems that some variables get initialized properly and some don't? I can't think of what could cause this... and it makes no difference if it's done on the main thread or not.

Thank you for your help.

What is your purpose of nesting struct ? When I try, I get 'free' not available in scope.

Yes. The ones I showed in the video were all initialized. I cannot find anything in the video which confirms all the static let variables are initialized. Do you know how to check static let variable is initialized?

Sorry for the vagueness. Been answering on an iPhone and I'm not the best typer.

Claude31 - the Structs are just a way to keep things organized. It was an incomplete typo on my side... The code should actually be written as

let newLives = purchasedApp ? Global.lives.paid : Global.lives.free

OOPer - The way I was checking (not shown I the video) is by running a (loadStaticVariableTest) function that has variables being assigned the values (ie var localLives = Global.lives.paid, etc... ) and setting a break point at the end of the function (I also checked by setting a break point on another line after calling the loadStaticVariableTest() function in a viewDidLoad event), then checking the values. Is there a way to check init and non-init lazy variables in debug?

UPDATE With additional troubleshooting, I see that this is only happening when running on a physical device, and NOT in the simulator. I still need to find another device to test on. But... could there be a compiler setting or some type of timing issue that doesn't allow for some lazy variables to be initialized? (iPAD OS is 14.7.1 on iPad Pro 10.5 inch) (simulator is 14.5 on iPad 9.7)

@blaine l, thanks for clarifying. And that is the one I was to propose to you. I will take time to check it on some actual devices. You should better send a bug report to Apple, attaching a project which reproduces the issue and the precise condition and steps-to-reproduce may be needed.

Thanks OOPer. I was going to upload the project for people to look at, but then got to borrow another iPad (iPad2). So now it looks like an isolated issue on the iPad I've been using (iPad1). It didn't happen on iPad2! As clear as night and day! I reset iPad1 to factory defaults and tested. And now it works fine?!? Now I'm going to reload the backup I made before resetting.

Why do Let Integer constants change values or always equal Zero?
 
 
Q