Skip to content

Pocket Swift Style Guide

Cyndi Chin edited this page Jul 6, 2023 · 7 revisions

Swift code should generally follow the conventions listed at https://github.com/raywenderlich/swift-style-guide.

Generally, we rely on SwiftLint to capture and ensure we follow certain Swift style and conventions. There are some rules that do not exist under the SwiftLint Rule Directory and we would to follow them for our codebase.

Internal By Default

If the access level is internal by default, then there is no need to mark it explicitly. In cases where this applies, please remove the internal keyword.

image

Keys

We should prefix keys with the function / action they're related to. For example, the key below is utilized for search in general, so we use Search. UPDATE: We have moved the hard coded strings for these keys to UserDefaultsKey.swift.

class SearchViewModel: ObservableObject {
    static let recentSearchesKey = "Search.recentSearches"

Using fatalError

As a rule of thumb, we would like to avoid crashing the application and using fatalError unless the application is unable to function without the piece of code (i.e. Core Data stack initialization). There is no existing rule in the SwiftLint directory, so as engineers let's be mindful when we see the usage of fatalError and if it is needed.