iOS best practices
- 1. Architecture
- 2. Name it right
- 3. Project structure
- 4. Functional Reactive Programming
- 5. Unidirectional data flow
- 6. Data manipulation
- 7. Prefer decoration over extension
- 8. Mixin
- 9. IoC container
- 10. Single Responsibility Principle
- 11. Performance
- 12. Main thread
- 13. Overlay app icon with build information
- 14. Show TODO FIXME as warnings
- 15. Breakpoints
- 16. Code snippets
- 17. Utility class
- 18. Xcode plugins
- 19. Xcode keyboard shortcuts
- 20. xcconfig
- 21. Strongly typed resource
- 22. Continuous learning
- 23. Reference
Here are practices I found useful when developing iOS.
They can be tools or framework that greatly affect your style. Sometimes, it applies to other platform as well
Architecture
We’re always looking for ways to structure our apps better, I collect some cool stuffs in ios-architecture
Name it right
- Name It Right
- Cocoa style for Objective C
- swift-style-guide
- swift-style-guide
Naming is hard. But we can start by naming correctly and with correct spelling. The best way is to stick with the platform naming convention
Project structure
Some prefer to group their projects by object role, like Model, View, View Controller. But I prefer to group by feature or use case, learning from Clean Architecture
- When you look at a software system, and all you see is MCV in a web configuration, then the architecture of that system is hiding the use cases of that system and exposing the delivery mechanism.
I often group my code like this
- Share: Contains code that can be useful to other project. Not related to any project. Can be extract to a Pod
- Engine: Contains code that is specific to this project. Like UserManager, ViewAnimator, …
- Modules: Separate each screen into module. In each module, there are view, view model and wireframe
There are some architectures like MVC, MVVM, VIPER, VIP, … and you may want to leverage some templates to help creating projects faster, like
And learn how to make them
- Custom Xcode File Templates
There are also some scripts to bootstrap your project
- crafter Crafter - Xcode project configuration CLI made easy
- xcake Create your Xcode projects automatically using a stupid simple DSL.
- liftoff CLI for creating and configuring new Xcode projects
Functional Reactive Programming
The Reactive Extension inspired libraries are cool, and they greatly changes the way you develop, especially signal chaining
Unidirectional data flow
Another trend is React inspired libraries, which encourage Unidirectional data flow
Data manipulation
Leverage some functional style for better data manipulation
- Swiftz It defines functional data structures, functions, idioms, and extensions that augment the Swift standard library.
- Dollar.swift A functional tool-belt for Swift Language similar to Lo-Dash or Underscore.js in Javascript
Prefer decoration over extension
This maybe subjective. But in some case decoration wins
Often when we want to add custom color, font or animate a view, or custom NSDateFormatter
, we often make categories on UIColor, UIFont, UIView, NSDateFormatter
, …
But most of the case, we can group them inside class, like ColorBucket, FontBucket, ViewAnimator, DateFormatterBucket
, …
So, instead of
1 |
@interface UIColor (Additions) |
We can have ColorBucket
1 |
@interface ColorBucket |
Mixin
- Mixins and Traits in Swift 2.0
Another way to add functionality to an entity is mixin, which is cool
IoC container
Usage of IoC container can greatly change the app workflow, which praises Dependency Injection
Single Responsibility Principle
Always keep this in mind :]
Performance
Understanding of performance issue, especially some common uses like tableview pre rendering, helps
- Easy UITableView optimizations
- 25 iOS App Performance Tips & Tricks
- iOS Quick Tip: 5 Tips to Increase App Performance
- Mastering UIKit performance
Main thread
It’s good to care about our main thread so see if it is blocked
- PSPDFUIKitMainThreadGuard.m This is a guard that tracks down UIKit access on threads other than main
- Watchdog Class for logging excessive blocking on the main thread
Overlay app icon with build information
Show TODO FIXME as warnings
- HOW TO HIGHLIGHT YOUR TODOS, FIXMES, & ERRORS IN XCODE
-
XToDo Xcode plugin to collect and list the
TODO
,FIXME
,???
,!!!!
Use this script in Run Script Build Phase
1 |
KEYWORDS="TODO:|VERIFY:|FIXME:|\?\?\?:|\!\!\!:" |
Breakpoints
There are some common needed breakpoints, like Breakpoints_v2.xcbkptlist
Code snippets
Code snippets auto generate common code
Utility class
We can avoid duplicated helper classes in projects by using some cool frameworks
- Sugar Sugar is a sweetener for your Cocoa implementations.
Xcode plugins
Using Xcode plugins can boost your productivity
And learning to write your own can assist you with more tasks
Xcode keyboard shortcuts
Navigate faster
xcconfig
Build yourself some predefined xcconfig
- Using xcconfig files for your XCode Project
- Warnings-xcconfig An xcconfig (Xcode configuration) file for easily turning on a boatload of warnings in your project or its targets.
- XcodeWarnings xcconfig to enable lots of Xcode warnings
Strongly typed resource
There’s some scripts that generate strongly typed resource classes
- SwiftGen A collection of Swift tools to generate Swift code (enums for your assets, storyboards, Localizable.strings, …)
- R.swift Get strong typed, autocompleted resources like images, fonts and segues in Swift projects
Continuous learning
Technologies move really fast. We must learn continuously. I have ios-resources to keep track of all the cool stuffs
Also, I leverage Github as my notes ios-issues