Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is DarwinBoolean type in Swift

Tags:

swift

I have written Boolean instead of Bool in some Swift code and Xcode offered me to replace it with DarwinBoolean.

The question is, what exactly is DarwinBoolean?

What are the differences comparing to Bool and ObjCBool types. What is it's purpose?

like image 754
Dalija Prasnikar Avatar asked Nov 12 '15 08:11

Dalija Prasnikar


1 Answers

Short answer:

  • Bool is the native Swift type for truth values.
  • DarwinBoolean is the Swift mapping of the "historic" C type Boolean.
  • ObjCBool is the Swift mapping of the Objective-C type BOOL.

You would use Bool in your Swift code unless one of the other types is required for interoperability with existing Core Foundation or Objective-C functions.


More about DarwinBoolean: DarwinBoolean is defined in Swift as

/// 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)
}

and is the Swift mapping of the "historic" C type Boolean from 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;

See also the Xcode 7 Release Notes:

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)

As an example, the functions

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

are imported to Swift as

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

In myFunc1 there is an automatic conversion between the native Swift type Bool and the Mac Type Boolean. This is not possible in myFunc2 because the address of a variable is passed around, here DarwinBoolean is exactly the Mac Type Boolean.

In previous versions of Swift – if I remember correctly – this mapped type was called Boolean, and has been renamed to DarwinBoolean later.


More about ObjCBool: ObjCBool is the Swift mapping of the Objective-C type BOOL, this can be signed char or the C/C++ bool type, depending on the architecture. For example, the NSFileManager method

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

is imported to Swift as

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

Here the BOOL return value is converted to Bool automatically, but the (BOOL *) is kept as UnsafeMutablePointer<ObjCBool> because it is the address of a variable.

like image 164
Martin R Avatar answered Nov 11 '22 16:11

Martin R