DEV Community

Evan Deaubl
Evan Deaubl

Posted on • Originally published at appsdissected.com

When is a Bool not a Bool?

This is a silly question — or rather, it should be a silly question — but it’s going to bite you sometimes when you integrate Objective-C and Swift code together (cough UIKit).

You will try to use what you think is a Bool from an Objective-C API, and suddenly you’re getting this compile error:

Cannot convert value of type ‘ObjCBool’ to expected argument type ‘Bool’

This is a Boolean value; why can’t I use this @($^ thing as a Bool?

Normally the standard types play nice between Objective-C and Swift. But Bools are an unusual case that require a non-obvious solution.

Objective-C uses BOOL as the type for Boolean values. In earlier versions of Swift, this was properly bridged to a Bool. In Swift 3, this changed to a value of type ObjCBool. When you’re used to the standard types being automatically bridged nicely, it’s jarring when suddenly you have what you think is a Bool, but it can’t be used like a Bool anywhere. You get the aforementioned error. It also cannot be cast to a Bool; that results in a compile-time error as well.

It turns out that there is a property on ObjCBool instances called boolValue, which is responsible for producing a Swift Bool from the ObjCBool. So, in the example I was having a problem with, accessing a BOOL property on an Objective-C object:

let val = objcObject.aBool // This is of type ObjCBool
let boolVal = objcObject.aBool.boolValue // This is actually a Bool

It’s aggravating when Swift itself isn’t very Swift-like, isn’t it?

If you need to go the other direction, that is thankfully more obvious:

let objectiveCBool = ObjCBool(swiftBoolVal)

Maybe one of these days we will be able to put Objective-C behind us all together. Until then, little workarounds like this are going to be part of the game.


Did you like this tip? The next tip on speeding up iOS UI code reviews by including screenshot or video diffs is already waiting for you. Or sign up to get every tip straight to your inbox.

This post originally published at Apps Dissected.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️