本文介绍了typecast BOOL在iOS 8.1.1中总是返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存&使用以下方法在字典中检索BOOL值。

I save & retrive the BOOL value in a dictionary using the following methods.

[userData setObject:@YES forKey:@"IS_AGENT"];

(BOOL) [userData objectForKey:@"IS_AGENT"];

在上面的代码中,从字典重试bool值我总是得到假在以下设备iPhone5s ,iPhone5c,iPhone6,iPhone6 +,它有iOS 8.1.1和相同的代码工作正常在iPod Touch有相同的iOS 8.1.1

In the above code while retrying the bool value from the dictionary I always get false in the following devices iPhone5s, iPhone5c, iPhone6, iPhone6+ which has iOS 8.1.1 and the same code works fine in iPod Touch which has the same iOS 8.1.1

google googled后我来知道我们不应该这样键入cast BOOL ,因为 BOOL char BOOL 挤压一个大于 char 的值,

After googled I came to know that we should not type cast BOOL like this because "BOOL is a char, which is eight bits. If you try to squeeze a value larger than a char through BOOL, the compiler will happily truncate the upper bits, slicing them off."

现在我解决了这个问题,只需发送 boolValue 消息到特定对象。检查下面的工作代码。

Now I fixed the issue simply sending boolValue message to a particular object. Check the working code below.

[[userData objectForKey:@"IS_AGENT"] boolValue];

好吧,我的问题是为什么会发生在特定的设备(iPhone5s,iPhone5c,iPhone5 ,iPhone6 +),而不是在iPod touch中有相同的iOS 8.1.1?

Ok fine.My question is "Why is it happening in a particular devices (iPhone5s,iPhone5c,iPhone5,iPhone6+) only and not in iPod touch which is having the same iOS 8.1.1?"

推荐答案

这是我的猜测发生了什么在iOS 8.1.1之前。也许苹果改变了标记指针的工作方式,我没有机会来测试它,还没有找到任何参考,无论苹果是否改变了这个系统中的东西。

Here's my speculation what happened before iOS 8.1.1. Maybe Apple changed the way tagged pointers work here, I haven't had an opportunity to test it yet and haven't found any references whether Apple did change something in this system.

您正在向BOOL投射指针。因为我们在这里讨论小端序系统,当你转换为 BOOL char c>你得到1,因为字节不是0.在64位的iOS系统与64位二进制,运行时使用

You were casting a pointer to a BOOL. Since we're talking about little endian systems here, a pointer like 0x12345678 casted to char yields 0x78, when you cast to BOOL you get 1 since the byte is not 0. On 64-bit iOS systems with 64-bit binaries, the runtime uses tagged pointers.

对于标记的指针,第一个字节(第一个字节)这是最低有效数字,我们是小端)将总是有最后一个位设置标记为一个带标记的指针。如果将该字节转换为BOOL,您将始终获得1 / YES。

For tagged pointers, the first byte (which are the least significant digits, we're little endian) will always have the last bit set to mark it as a tagged pointer. If you cast that byte to BOOL you'll always get 1/YES.

但对于未标记的指针,由于对齐,最后四位始终为0。偶然的,它可能发生的其他位是0太(在我的测试中,我只看到的0x20的倍数,所以每8个对象将有0x00)。当你把它转换为 BOOL 时,你大部分时间都会得到YES,有时候是NO。

But for non-tagged pointers, due to aligning, the last four bits are always 0. And by chance it can happen that the other bits of that are are 0 too (in my tests I've only seen multiples of 0x20 so every 8th object would have 0x00). When you cast that to a BOOL you'll get YES most of the time and sometimes NO.

已经发现,查询值的正确方法是 [[userData objectForKey:@IS_AGENT] boolValue] 或简单地 [userData [@] IS_AGENT] boolValue] 如果您的部署目标是iOS> = 6。

As you already found out, the correct way to query the value is [[userData objectForKey:@"IS_AGENT"] boolValue] or simply [userData[@"IS_AGENT"] boolValue] if your deployment target is iOS >= 6.

这篇关于typecast BOOL在iOS 8.1.1中总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!