What is DarwinBoolean type in Swift
Short answer:
Bool
is the native Swift type for truth values.DarwinBoolean
is the Swift mapping of the "historic" C typeBoolean
.ObjCBool
is the Swift mapping of the Objective-C typeBOOL
.
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.
Cannot convert value of type 'Int' to raw type 'DarwinBoolean'
Ok so we have fixed this issue. The error message proved to be quite unhelpful as expected.
The issue turned out to be quite a stupid one. The orderService
mentioned above had a method declared in the .h
file which was unimplemented in the .m
file and never used. The fact that it was unimplemented seems to have triggered the compiler error with the incredibly unhelpful message about Int
not being convertible to DarwinBoolean
.
Removing the declaration in the header file (or implementing the unneeded methods) solved the issue.
Swift Booleans not working: Cannot assign value of type 'Bool' to type 'BOOL' (aka 'Int32')
The solution for this problem was to disable optimizations in the build settings (from "optimize for speed" to "no optimization").
Cannot assign value of type UnsafeMutablePointer ObjCBool in Swift
I've never seen anything like the situation you describe, and personally I'm tempted to say the situation doesn't exist; I have never seen a BOOL*
property or ivar in Objective-C in my life (and I'm darned old, believe me). However, if you insist: I haven't tested this, but I think you could say something like this:
var ok = UnsafeMutablePointer<ObjCBool>.alloc(1)
ok[0] = false // or true
let val = ok
myClass.isSSNField = val
However, although I think that will compile, I'm rather unclear on what the implications of doing it would be. It could cause the universe to collapse to a black hole, so be careful.
Swift closure: Cannot convert value of type '(_) - Bool' to expected argument type
Something flakey is happening with Swift's type inference. Give card
an explicit type and it will work.
return hand.filter({ (card: Card) -> Bool in return card.type == .Red })
You don't need the return type or the return:
return hand.filter({ (card: Card) in card.type == .Red })
Note: this works also:
return hand.filter({ ($0 as Card).type == .Red })
Fully specifying the .Red
enum value resolves the issue as well:
return hand.filter({ $0.type == Card.CardType.Red })
It was mentioned in the comments that if you move the definition of Card
into the same file as the filter, that it works. In fact, if you split the definition of the CardType
enum out from Card
and just move CardType
into the file with the filter, it works.
Representing NULL Function Pointers to C Functions in Swift
The Swift mapping of the (Objective-)C declaration
extern void _NSSetLogCStringFunction(void(*)(const char*, unsigned, BOOL));
is
public func _NSSetLogCStringFunction(_: (@convention(c) (UnsafePointer<Int8>, UInt32, ObjCBool) -> Void)!)
The easiest solution would be to put the Objective-C extern
declaration into an Objective-C header file and include that
from the bridging header.
Alternatively, in pure Swift it should be
typealias NSLogCStringFunc = @convention(c) (UnsafePointer<Int8>, UInt32, ObjCBool) -> Void
@_silgen_name("_NSSetLogCStringFunction")
func _NSSetLogCStringFunction(_: NSLogCStringFunc!) -> Void
In either case, the function parameter is an implicitly unwrapped optional,
and you can call it with nil
. Example:
func myLogger(message: UnsafePointer<Int8>, _ length: UInt32, _ withSysLogBanner: ObjCBool) -> Void {
print(String(format:"myLogger: %s", message))
}
_NSSetLogCStringFunction(myLogger) // Set NSLog hook.
NSLog("foo")
_NSSetLogCStringFunction(nil) // Reset to default.
NSLog("bar")
Output:
myLogger: foo
2016-04-28 18:24:05.492 prog[29953:444704] bar
How to retrieve a value from Firestore to change the text of button in table cell for every listen item?
Add something like this on the top of cellForRowAt
let thisComment = self.comments[indexPath.row]
let checkl1value = thisComment.checkl1value
Comments not Purple coloured when using /// in Xcode (swift)
The color of the documentation markups can be found in the Xcode preferences (tab "Fonts & Colors", line "Documentation Markup").
Related Topics
How to Create a Hotspot Network in iOS App Using Swift
Manually Disposing a Disposebag in Rxswift
How to Change Text Color of Actionsheet in Swiftui
Closure:Use Unresolved Identifier 'Self'
How to Make Function That Some Parameters Not Required in When Call It in iOS Swift 3
Saving a Codable Struct to Userdefaults with Swift
How to Get Walking and Running Distance Using Healthkit in Swift
Xcode Generated Coredata Files Cannot Be Processed by Copy Bundle Resources Build Phase
Swiftui Multiline Text Background Color
Swift:Pause and Resume Nstimer
Swift: How to Flush Stdout After Println
Unwrapping Optional in Swiftui View
Create a Navigationlink Without Back Button Swiftui
Zposition of Sknode Relative to Its Parent
How to Add Document with Custom Id to Firebase (Firestore) on Swift