← All notes

Swift optionals, nil, binding, and force unwrapping

Short answer

You use optionals in situations where a value may be absent [S1]. Write an optional type with a question mark after the type name, as in Int? [S1]. Optional is an enumeration with two cases, .none and .some [S2]. Optional binding extracts a value; force unwrapping a nil value triggers a runtime error [S1].

let possibleNumber = "42"
if let number = Int(possibleNumber) {
    print("Value: \(number)")
} else {
    print("Not a number")
}

Key points

Pitfalls

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources and notices: The Swift Programming Language (swiftlang/swift-book): Copyright (c) 2014-2026 Apple Inc. and the Swift project authors; Apache-2.0 (https://raw.githubusercontent.com/swiftlang/swift-book/main/LICENSE.txt). Swift standard library (swiftlang/swift): Copyright (c) 2014-2026 Apple Inc. and the Swift project authors; Apache-2.0 with Runtime Library Exception (https://raw.githubusercontent.com/swiftlang/swift/main/LICENSE.txt). Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Swift Programming Language, The Basics, Apache-2.0 (swiftlang/swift-book); Optional.swift, Apache-2.0 with Runtime Library Exception (swiftlang/swift). Copyright (c) 2014-2026 Apple Inc. and the Swift project authors. Notices: The Swift Programming Language (swiftlang/swift-book): Copyright (c) 2014-2026 Apple Inc. and the Swift project authors; Apache-2.0 (https://raw.githubusercontent.com/swiftlang/swift-book/main/LICENSE.txt). Swift standard library (swiftlang/swift): Copyright (c) 2014-2026 Apple Inc. and the Swift project authors; Apache-2.0 with Runtime Library Exception (https://raw.githubusercontent.com/swiftlang/swift/main/LICENSE.txt).

Cite this

Permanent ID: iil:note:3yzgzex7

iinventedlight. 2026. "Swift optionals, nil, binding, and force unwrapping." iinventedlight Library, iil:note:3yzgzex7, version of 2026-11-11. https://iinventedlight.com/library/notes/swift-optionals/. License: CC-BY-4.0.

@misc{iil_note_3yzgzex7,
  author = {{iinventedlight}},
  title = {{Swift optionals, nil, binding, and force unwrapping}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-11},
  url = {https://iinventedlight.com/library/notes/swift-optionals/},
  note = {Permanent ID iil:note:3yzgzex7. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/swift-optionals.md.