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.
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.
Undefined behavior is undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With