I have a textView and a button. Button's enabled property is related to length of text in textView. I assign NSUInteger to BOOL directly which is proved to be unsuitable now.
- (void)textViewDidChange:(UITextView *)textView {
self.button.enabled = textView.text.length; //unsuitable on 32-bit. Should be `textView.text.length > 0`
}
The strange thing on 32-bit devices : If I paste "abc" into textView, or paste "ab" then type "c", the button is always disabled. But if I type these three letters one by one, the button performs as it should be. All of the 3 situations reach self.button.enabled = (BOOL)3, but why the performance of button is different?
Type of BOOL
The differing behaviour is actually linked to the device's architecture. There's a difference on how the iOS SDK defines a BOOL for 32 versus 64 bit architectures. See this excerpt from objc.h:
#if !defined(OBJC_HIDE_64) && TARGET_OS_IPHONE && __LP64__
typedef bool BOOL;
#else
typedef signed char BOOL;
#endif
So BOOL on 32 bit can have any value between 0 and 255, while on 64 bit it is compiler enforced to only have the values 0 or 1. You can easily try this by running the following line on the simulator, set to iPhone 4s (32 bit) or iPhone 6 (64 bit).
NSLog(@"%d", (BOOL)2);
That is why you see differing behavior on different devices.
EDIT : Post OP comment:
Well to understand that, you need to see how enabled property is laid down : unsigned int enabled:1. This defines the bit-field of width one.
On 32 bit systems, per C standards, when assigning an unsigned integer of greater width the remaining bits are just dropped. So the lowest bit is what actually drives the enabling/disabling of button.
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