NSNumber(booleanLiteral: true) alternate syntax
Asked on 10/03/2024
1 search
In Swift, NSNumber(booleanLiteral: true)
is a way to create an NSNumber
object that represents a boolean value. However, in Swift, you can use a more straightforward syntax to represent boolean values directly using Swift's native Bool
type. For example, you can simply use true
or false
as boolean literals.
If you are working with Swift and need to use NSNumber
for compatibility with Objective-C APIs, you can create an NSNumber
from a Bool
like this:
let number = NSNumber(value: true)
This approach is more idiomatic in Swift when you need to bridge between Swift and Objective-C.
For more information on Swift's features and design, you might find the session A Swift Tour: Explore Swift’s features and design helpful, which covers various aspects of Swift, including value types and mutability.

A Swift Tour: Explore Swift’s features and design
Learn the essential features and design philosophy of the Swift programming language. We’ll explore how to model data, handle errors, use protocols, write concurrent code, and more while building up a Swift package that has a library, an HTTP server, and a command line client. Whether you’re just beginning your Swift journey or have been with us from the start, this talk will help you get the most out of the language.

What’s new in AppKit
Discover the latest advances in Mac app development. Get an overview of the new features in macOS Sequoia, and how to adopt them in your app. Explore new ways to integrate your existing code with SwiftUI. Learn about the improvements made to numerous AppKit controls, like toolbars, menus, text input, and more.

Catch up on accessibility in SwiftUI
SwiftUI makes it easy to build amazing experiences that are accessible to everyone. We’ll discover how assistive technologies understand and navigate your app through the rich accessibility elements provided by SwiftUI. We’ll also discuss how you can further customize these experiences by providing more information about your app’s content and interactions by using accessibility modifiers.