Objective-C:BOOL vs bool

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

问题描述

我看到了新类型"BOOL(YESNO).

I saw the "new type" BOOL (YES, NO).

我读到这种类型几乎就像一个字符.

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 ?

我可以只使用 bool(这似乎有效)而不会降低速度吗?

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)

所以,是的,您可以假设 BOOL 是一个字符.您可以使用 (C99) bool 类型,但 Apple 的所有 Objective-C 框架和大多数 Objective-C/Cocoa 代码都使用 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 bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Why local notification is not firing for UNCalendarNotificationTrigger(为什么没有为UNCalendarNotificationTrigger触发本地通知)
iOS VoiceOver functionality changes with Bundle Identifier(IOS画外音功能随捆绑包标识符而变化)
tabbar middle tab out of tabbar corner(选项卡栏中间的选项卡角外)
Pushing UIViewController above UITabBar(将UIView控制器推送到UITabBar上方)
How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)