Skip to content

Commit 097ea3d

Browse files
committed
Update post-21
1 parent c64cfba commit 097ea3d

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed
59.8 KB
Loading

content/en/posts/post-21/index.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
---
22
date: 2024-10-16
3-
title: "Mastering Swift Assertions: Choosing the Right Tool for the Job"
3+
title: "Swift Assertions Cheatsheet: How, Why, and When to Crash"
44
slug: mastering-swift-assertions
5-
images: [""]
5+
images: ["https://i.imgflip.com/96ybn9.jpg"]
66
description: Should you use assert(), precondition(), or maybe fatalError()? Let's learn how to decide.
7-
topics: ["Swift"]
7+
topics: ["Swift", "Cheatsheet"]
88
---
99

10-
# Swift Assertions: Choosing the Right Tool for the Job
10+
# Swift Assertions Cheatsheet: How, Why, and When to Crash
11+
<center>
12+
<img src="https://i.imgflip.com/96ybn9.jpg" alt="Chaos Girl Meme: Swift watches while a house labeled `fatalError()` burns.">
13+
</center>
1114

1215
As Swift developers, we have several assertion tools at our disposal. But how do we choose the right one for each situation? This blog post will explore the different types of assertions in Swift and provide a framework to help you decide which to use and when.
1316

@@ -29,13 +32,6 @@ For this reason, Swift provides us with several tools:
2932
`assert()` is used to check a condition that must be true for your code to continue execution. If the condition is false, the assertion triggers and the program terminates. But the program will only terminate if this was a debug build. If you build the same code for production then Swift will ignore the `assert()` and move on.
3033

3134
```swift
32-
/// Divides two integers.
33-
///
34-
/// - Parameters:
35-
/// - a: The numerator.
36-
/// - b: The denominator.
37-
/// - Returns: The result of the division.
38-
/// - Precondition: `b` must not be zero.
3935
func divide(_ a: Int, by b: Int) -> Int {
4036
assert(b != 0, "Cannot divide by zero")
4137
return a / b
@@ -102,7 +98,9 @@ class Animal {
10298
}
10399
```
104100

105-
Sometimes a class will implement a function with a fatalError if they require subclasses to override it. Another common use case is to fatal error when the app is unable to load the database upon startup. This is a very common pattern when using Core Data for example.
101+
Sometimes a class will implement a function with a fatalError if they require subclasses to override it. However, I prefer not to use this pattern because it's easy to use this API wrong. For example, in many classes it is expected that you finish an implementation by calling the parent implementation (e.g. `super.makeSound()`). This pattern is used throughout UIKit for example. However, if we called `super.makeSound()` here, it would crash. The fact that it would crash is not obvious at the call site, or the definition. Instead we have to pay close attention to the documentation.
102+
103+
Another common use case for `fatalError()` is when the app is unable to load its database upon startup. If the app can't use its database, much if not all of its functionality is broken. It's better to crash. This is a very common pattern when using Core Data for example.
106104

107105
## Crashes vs. Errors
108106

@@ -148,5 +146,5 @@ Today we learned the value of crashing. We also learned how, why and when to cra
148146
- [Addressing crashes from Swift runtime errors](https://developer.apple.com/documentation/xcode/addressing-crashes-from-swift-runtime-errors)
149147
- [Analyzing a crash report](https://developer.apple.com/documentation/xcode/analyzing-a-crash-report)
150148
- [Error Handling](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/errorhandling/)
151-
- [SwiftRocks - How To Solve Any iOS Crash Ever](https://swiftrocks.com/how-to-solve-any-ios-crash-ever)
152-
- [SwiftLee - EXC_BAD_ACCESS crash error: Understanding and solving it](https://www.avanderlee.com/swift/exc-bad-access-crash/)
149+
- [SwiftLee - EXC_BAD_ACCESS crash error: Understanding and solving it](https://www.avanderlee.com/swift/exc-bad-access-crash/)
150+
- [SwiftRocks - How To Solve Any iOS Crash Ever](https://swiftrocks.com/how-to-solve-any-ios-crash-ever)

0 commit comments

Comments
 (0)