Objective-C的:BOOL VS布尔 [英] Objective-C : BOOL vs bool

查看:209
本文介绍了Objective-C的:BOOL VS布尔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了新型 BOOL NO )。

我读到这一类几乎就像一个char。

I read that this type is almost like a char.

有关测试我所做的:

NSLog(@"Size of BOOL %d", sizeof(BOOL));
NSLog(@"Size of bool %d", sizeof(bool));

高兴地看到,这两个日志显示1(有时在C ++中BOOL是int和它的sizeof为4)

Good to see that both logs display "1" (sometimes in C++ bool is an int and its sizeof is 4)

所以,我只是想知道,如果有一些问题,bool类型什么的?

So I was just wondering if there were some issues with the bool type or something ?

我可以只使用布尔(即似乎工作)不失速度?

Can I just use bool (that seems to work) without losing speed?

推荐答案

从定义 objc.h

#if (TARGET_OS_IPHONE && __LP64__)  ||  TARGET_OS_WATCH
typedef bool BOOL;
#else
typedef signed char BOOL; 
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" 
// even if -funsigned-char is used.
#endif

#define YES ((BOOL)1)
#define NO  ((BOOL)0)

所以,是的,你可以假设布尔是一个字符。您可以使用(C99)布尔类型,但所有苹果的Objective-C框架和大部分的Objective-C /可可code使用BOOL,所以你可以节省自己头痛,如果有史以来的typedef仅仅使用BOOL变化。

So, yes, you can assume that BOOL is a char. You can use the (C99) bool type, but all of Apple's Objective-C frameworks and most Objective-C/Cocoa code uses BOOL, so you'll save yourself headache if the typedef ever changes by just using BOOL.

这篇关于Objective-C的:BOOL VS布尔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆