This guide is built to share the best practices in writing swift code.
- Use english to code.
- Use camel case. For example:
let isCorrect: Bool
- Use descriptive code, clarity is more important than brevity. For example:
let isFriday: Bool {} if isFriday { print("Friday!!!!") }
- https://swift.org/documentation/api-design-guidelines/
- https://github.com/raywenderlich/swift-style-guide
-
Swiftlint is a tool to enforce swift style and conventions, here the link to its github https://github.com/realm/SwiftLint/.
-
Here are the set of rules that we use:
disabled_rules:
- control_statement
- line_length
- trailing_whitespace
- force_cast
- class_delegate_protocol
- weak_delegate
opt_in_rules:
- empty_count
excluded:
- Pods
vertical_whitespace:
max_empty_lines: 2
file_length:
warning: 500
error: 1000
There are many architectures developers use to build IOS apps, MVC (Model View Controller), is Apple recommended architecture pattern.
- Here's a Icalia Labs blog by Daniel Lozano which we recommend reading: Introduction to iOS App Architectures
Many IOS applications use external Api, this is a setup which might come useful.
- This setup uses a singleton class, ApiClient in which the api methods are written.
class ApiClient {
static let shared = ApiClient()
...
}
- Its also useful to divide the url endpoints to the base url
enum Endpoints {
case .getNames
...
}
- The methods use completion handlers. Something like this:
func getNames(completion: @escaping (Result(T))) {
...
if error != nil {
completion(.error)
} else {
completion(.success(names))
}
}
Building views from xib's can be useful, to do this we recommend following steps shown in this post, Loading views from XIB’s on iOS, from Icalia Labs blog by Daniel Lozano.
- As for folder structure, follow something like this:
- Models
- Views
- Controllers
- Others
-
CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects. It has over 40 thousand libraries and is used in over 2.8 million apps. CocoaPods can help you scale your projects elegantly. (Taken from https://cocoapods.org)
-
Pods of Icalia Labs:
-
Pods that we use in Icalia Labs:
- Swift Packet Manager is used to create swift libraries and and command line apps in macOS.
mkdir CommandLineApp
cd CommandLineApp
swift package init --type executable