Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - Different Default Boolean Value on Device

I've recently had a bit of code work perfectly on the simulator, and screw up on device. Why? - It's very simple, but I can't explain the explantion.

I did a little testing, by creating a new view-based app, added a label and an outlet for the label, and wrote this code in the viewDidLoad:

BOOL b;
if (b) {
    label.text = @"Value = YES";
}
else {
    label.text = @"Value = NO";
}

Interestingly enough, here are the results:

iOS Simulator (4.2): Value = NO
iOS Simulator (4.3): Value = NO
iPod Touch 2G (4.2.1) Value = YES
iPhone 3G (4.2.1) Value = YES

What does this suggest? - Well to me it looks like that on devices, the default boolean value is YES, and on the simulator, the default boolean value is NO.

Can anyone clarify if this is the case? Also if someone could explain that behavior I would be very interested.

I am not interested in solutions to the problem, clearly it can be solved by setting the desired default (in my case NO) manually.

Thanks.

like image 747
Alex Coplan Avatar asked Jan 18 '23 04:01

Alex Coplan


2 Answers

Objective-C is just a superset of C, and in C declaring a variable like this just gives it memory on the stack. The variable isn't set to anything when it is declared, so whatever was last on the stack is now in your variable b.

In short, it's undefined, and will vary from implementation to implementation. That's what the spec says.

like image 192
joerick Avatar answered Jan 28 '23 20:01

joerick


Undefined behavior is undefined.

like image 25
Johan Kool Avatar answered Jan 28 '23 21:01

Johan Kool