[英]Assign NSUInteger to UIButton enabled property not work in devices with 32 bit architecture
Just come cross a strange bug in devices with 32 bit architecture, below is the code: 刚遇到32位架构的设备中的一个奇怪的错误,下面是代码:
- (void)textViewDidChange:(UITextView *)textView {
self.button.enabled = textView.text.length;
}
So whenever I paste some text whose length is not 1 into the textView, (this is to imitate the behaviour of a third-party keyboard Sogou ), the button will not be enabled. 因此,每当我将一些长度不为1的文本粘贴到textView中时(这是为了模仿第三方键盘Sogou的行为),都不会启用该按钮。
You can use this Demo to reproduce the bug. 您可以使用此演示重现该错误。
I've read related posts, like Assigning NSUInteger to BOOL conceptual understanding , but this case is different. 我已经阅读了相关文章,例如将NSUInteger分配给BOOL概念理解 ,但是这种情况有所不同。
Please tell me why it does not work here. 请告诉我为什么它在这里不起作用。
Don't think of it as a bug. 不要将其视为错误。 Think of it as your code being wrong. 认为这是您的代码错误。 This line is horrible: 这行太可怕了:
self.button.enabled = textView.text.length;
You are trying to misuse a number as a BOOL. 您正在尝试将数字误用为BOOL。 You are thus assuming that any non-zero number is the same as YES
. 因此,您假设任何非零数字都与YES
相同。 That is a false assumption. 这是一个错误的假设。 Stop writing code like that. 别再这样写代码了。 Write it like this: 像这样写:
self.button.enabled = (textView.text.length > 0);
That way, the BOOL really is a BOOL. 这样,BOOL确实是 BOOL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.