当我将 BOOL 用于 32 位时,我得到:
BOOL b1=8960; //b1 == NO
bool b2=8960; //b2 == true
但是对于 64 位,我得到:
BOOL b1=8960; //b1 == YES
bool b2=8960; //b2 == true
BOOL 从 32 位到 64 位有何变化?
最佳答案
@TimBodeit 是对的,但它没有解释为什么...
BOOL b1=8960; //b1 == NO
... 计算结果为 NO在 32 位 iOS 上以及为什么它的计算结果为 YES在 64 位 iOS 上。让我们从同一个开始。
ObjC BOOL 定义
#if (TARGET_OS_IPHONE && __LP64__) || (__ARM_ARCH_7K__ >= 2)
#define OBJC_BOOL_IS_BOOL 1
typedef bool BOOL;
#else
#define OBJC_BOOL_IS_CHAR 1
typedef signed char BOOL;
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C"
// even if -funsigned-char is used.
#endif
对于 64 位 iOS 或 ARMv7k( watch ),它被定义为 bool其余为 signed char .
ObjC BOOL YES 和 NO
阅读 Objective-C Literals ,在哪里可以找到:
Previously, the
BOOLtype was simply a typedef forsigned char, andYESandNOwere macros that expand to(BOOL)1and(BOOL)0respectively. To support@YESand@NOexpressions, these macros are now defined using new language keywords in<objc/objc.h>:
#if __has_feature(objc_bool)
#define YES __objc_yes
#define NO __objc_no
#else
#define YES ((BOOL)1)
#define NO ((BOOL)0)
#endif
The compiler implicitly converts
__objc_yesand__objc_noto(BOOL)1and(BOOL)0. The keywords are used to disambiguate BOOL and integer literals.
bool 定义
bool是在 stdbool.h 中定义的宏它扩展到 _Bool , 这是 C99 中引入的 bool 类型。它可以存储两个值,0或 1 .没有其他的。更准确地说,stdbool.h定义了四个要使用的宏:
/* Don't define bool, true, and false in C++, except as a GNU extension. */
#ifndef __cplusplus
#define bool _Bool
#define true 1
#define false 0
#elif defined(__GNUC__) && !defined(__STRICT_ANSI__)
/* Define _Bool, bool, false, true as a GNU extension. */
#define _Bool bool
#define bool bool
#define false false
#define true true
#endif
#define __bool_true_false_are_defined 1
_Bool
_Bool在 C99 中引入,它可以保存值 0或 1 .重要的是:
When a value is demoted to a
_Bool, the result is0if the value equals0, and1otherwise.
现在我们知道这个困惑是从哪里来的,我们可以更好地了解发生了什么。
64 位 iOS || ARMv7k
BOOL -> bool -> _Bool (值 0 或 1)
降级8960至_Bool给 1 , 因为该值不等于 0 .请参阅(_Bool 部分)。
32 位 iOS
BOOL -> signed char (值 -128 到 127)。
如果您要存储 int值( -128 到 127 )为 signed char ,根据 C99 6.3.1.3 的值不变.否则它是实现定义(C99 引用):
Otherwise, the new type is signed and the value cannot be represented in it; either the result is implementation-defined or an implementation-defined signal is raised.
这意味着clang可以决定。简而言之,使用默认设置,clang 将其包裹起来( int -> signed char ):
-129变成 127 ,-130变成 126 ,-131变成 125 ,反方向:
128变成 -128 ,129变成 -127 ,130变成 -126 ,但是因为signed char可以存储 -128 范围内的值至127 ,可以存储0也是。例如 256 ( int ) 变为 0 (signed char)。而当你的值(value)8960缠绕在...
8960变成 0 ,8961变成 1 ,8959变成 -1 ,...变成0当存储在 signed char 中时( 8960 是 256 的倍数,8960 % 256 == 0 ),因此它是 NO .这同样适用于 256 , 512 , ... 256 的倍数.
我强烈推荐使用 YES , NO与 BOOL并且不依赖像 int 这样的花哨的 C 功能作为 if 中的条件等等。这就是 Swift 拥有 Bool 的原因。 , true , 和 false你不能使用Int条件下的值 Bool是期待。只是为了避免这种困惑 ...
关于ios - iOS 上 64 位的 BOOL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31267325/
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里
print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上
为了问这样的问题:MyClass::create().empty?如何在MyClass中设置empty?Empty(true/false)取决于类变量@arr是否为空。 最佳答案 问号实际上是方法名称的一部分,因此您可以这样做:classMyClassdefempty?@arr.empty?#Implicitlyreturned.endend 关于ruby-Ruby中的bool方法?,我们在StackOverflow上找到一个类似的问题: https://st
我正在尝试复制此GETcurl请求:curl-D--XGET-H"Authorization:BasicdGVzdEB0YXByZXNlYXJjaC5jb206NGMzMTg2Mjg4YWUyM2ZkOTY2MWNiNWRmY2NlMTkzMGU="-H"Content-Type:application/json"http://staging.example.com/api/v1/campaigns在Ruby中,通过电子邮件+apikey生成身份验证:auth="Basic"+Base64::encode64("test@example.com:4c3186288ae23fd9661c
我正在向我的Controller发送一个base64图像并按原样保存它。现在我需要显示该图像。这是我要显示的内容,但未显示图像:"/>为了编码,我使用了这个java脚本函数encodeURIComponent();我的编码图像格式:data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/........ 最佳答案 你不需要解码base64应该可以 关于ruby-on-rails-在rails中显示base64编码的图像,我们在StackOve