什么是Swift中的DarwinBoolean类型

我在一些 Swift代码中写了Boolean而不是Bool,Xcode提供了我用DarwinBoolean替换它.

问题是,DarwinBoolean是什么?

与Bool和ObjCBool​​类型相比有什么区别?
这是什么目的?

简答:

> Bool是真值的本地Swift类型.
> DarwinBoolean是“历史”C类布尔值的Swift映射.
> ObjCBool​​是Objective-C类型BOOL的Swift映射.

您可以在Swift代码中使用Bool,除非其他类型之一
是与现有核心基金会的互操作性所必需的
Objective-C功能.

关于DarwinBoolean的更多信息
DarwinBoolean在Swift中定义为

/// The `Boolean` type declared in MacTypes.h and used throughout Core
/// Foundation.
///
/// The C type is a typedef for `unsigned char`.
public struct DarwinBoolean : BooleanType,BooleanLiteralConvertible {
    public init(_ value: Bool)
    /// The value of `self`,expressed as a `Bool`.
    public var boolValue: Bool { get }
    /// Create an instance initialized to `value`.
    public init(booleanLiteral value: Bool)
}

并且是Swift映射的“历史”C类布尔值
MacTypes.h

/********************************************************************************

    Boolean types and values

        Boolean         Mac OS historic type,sizeof(Boolean)==1
        bool            Defined in stdbool.h,ISO C/C++ standard type
        false           Now defined in stdbool.h
        true            Now defined in stdbool.h

*********************************************************************************/
typedef unsigned char                   Boolean;

另请参见Xcode 7发行说明:

The type Boolean in MacTypes.h is imported as Bool in contexts that
allow bridging between Swift and Objective-C types.

In cases where the representation is significant,Boolean is imported
as a distinct DarwinBoolean type,which is BooleanLiteralConvertible
and can be used in conditions (much like the ObjCBool type).
(19013551)

作为一个例子,功能

void myFunc1(Boolean b);
void myFunc2(Boolean *b);

被导入斯威夫特

public func myFunc1(b: Bool)
public func myFunc2(b: UnsafeMutablePointer<DarwinBoolean>)

在myFunc1中有一个本地的自动转换
Swift类型Bool和Mac类型Boolean.
这在myFunc2中是不可能的,因为变量的地址
被传递,这里DarwinBoolean是Mac Type Boolean.

在以前版本的Swift中 – 如果我记得正确 – 这个映射
类型被称为布尔值,并且稍后已被重命名为DarwinBoolean.

更多关于ObjCBool​​:
ObjCBool​​是Objective-C类型BOOL的Swift映射,
这可以是char或C/C++的bool类型,取决于
建筑.例如NSFileManager方法

- (BOOL)fileExistsAtPath:(Nsstring *)path
         isDirectory:(BOOL *)isDirectory

被导入Swift as

func fileExistsAtPath(_ path: String,isDirectory isDirectory: UnsafeMutablePointer<ObjCBool>) -> Bool

这里BOOL返回值自动转换为Bool,但是(BOOL *)保持为UnsafeMutablePointer< ObjCBool​​>因为它是变量的地址.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...