Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Code Patterns From the Ranch

Mike Zornek
September 09, 2016

Swift Code Patterns From the Ranch

At the Big Nerd Ranch we've been experimenting with Swift since it was first announced. We've also been lucky enough to have some wonderful clients whom have empowered us to use Swift on their projects. This talk will explore some of the way we've embraced Swift patterns as we interact with UIKit on our projects. We'll review some small ideas and then move up in scale to finish with some larger ideas. With luck you'll walk away with some actionable patterns to make your own Swift projects safer, easier to read and easier to test.

http://cocoaconf.com/dc-2016/sessions/Swift-Code-Patterns-From-the-Ranch

Mike Zornek

September 09, 2016
Tweet

More Decks by Mike Zornek

Other Decks in Programming

Transcript

  1. Swift Strongly Typed Value Types Composition Protocol-Oriented Programming Objective-C /

    UIKit Weakly Typed Reference Types Inheritance Object-Oriented Programming
  2. class Car { let driver: Driver init() { } //

    Return from initializer without // initializing all stored properties. }
  3. // Injectable is a simple protocol to helps enforce Dependency

    Injection. // It is typically used on View Controllers where you don't have early // life-cycle access and need to inject or configure required properties // after the object has been initialized. protocol Injectable { // When honoring this protocol we expect you to make a method called // inject() that has the needed properties for this object. // checkDependencies is a method intended to verify that our // implicitly unwrapped optionals preconditions have been populated. // It should called in an early life-cycle method where the // required dependancies should have already been set. // For View Controllers, viewDidLoad() is a common choice. func checkDependencies() }
  4. class WeatherDisplayViewController: UIViewController { var weatherService: WeatherService! var location: Location!

    override func viewDidLoad() { super.viewDidLoad() checkDependencies() updateUI() } } //MARK: - Injectable extension WeatherDisplayViewController: Injectable { func inject(weatherService: WeatherService, location: Location) { self.weatherService = weatherService self.location = location } func checkDependencies() { precondition(weatherService != nil) precondition(location != nil) } }
  5. extension UserDefaults { enum Key { static let themeIdentifier =

    "com.ranchweather.userdefaults.theme" } enum Notifications { static let themeDidChange = Notification.Name("com.ranchweather.notification.themeDidChange") } } string(forKey: Key.themeIdentifier) NotificationCenter.default.post(name: Notifications.themeDidChange, object: self)
  6. extension UIImage { enum Asset: String { case clearDay =

    "clear-day" case clearNight = "clear-night" case rain = "rain" case snow = "snow" case sleet = "sleet" case wind = "wind" case fog = "fog" case cloudy = "cloudy" case partlyCloudyDay = "partly-cloudy-day" case partlyCloudyNight = "partly-cloudy-night" } convenience init!(asset: Asset) { self.init(named: asset.rawValue) } } UIImage(asset: .snow)
  7. extension UIStoryboard { private enum File: String { case main

    = "Main" case weatherDisplay = "WeatherDisplay" case feedback = "Feedback" case debugMenu = "DebugMenu" } private convenience init(_ file: File) { self.init(name: file.rawValue, bundle: nil) } } let storyboard = UIStoryboard(.weatherDisplay)
  8. extension UIStoryboard { private enum Identifier: String { ... }

    private convenience init(_ identifier: Identifier) { self.init(name: identifier.rawValue, bundle: nil) } } let storyboard = UIStoryboard(.WeatherDisplay)
  9. extension UIStoryboard { static func weatherDisplayViewController() -> WeatherDisplayViewController { let

    storyboard = UIStoryboard(.weatherDisplay) return storyboard.instantiateInitialViewController() as! WeatherDisplayViewController } } let vc = UIStoryboard.weatherDisplayViewController()
  10. extension UIStoryboard { static func debugViewControllerStack(configure: (DebugMenuViewController) -> Void) ->

    UINavigationController { let navigationController = UIStoryboard(.DebugMenu). instantiateInitialViewController() as! UINavigationController let debugMenu = navigationController.viewControllers[0] as! DebugMenuViewController configure(debugMenu) return navigationController } } let debugNavigationController = UIStoryboard.debugViewControllerStack { (debugVC) in debugVC.inject(userDefaults: UserDefaults.standard, delegate: self) } present(debugNavigationController, animated: true, completion: nil)
  11. struct WeatherService { func fetchWeatherReport(latitude: Double, longitude: Double, completion: @escaping

    (_ result: WeatherService.Result) -> Void) { dataSource.fetchWeatherReport(latitude: latitude, longitude: longitude, completion: completion) } }
  12. struct WeatherService { enum Result { case success(WeatherReport) case failure(WeatherService.Error)

    } func fetchWeatherReport(latitude: Double, longitude: Double, completion: @escaping (_ result: WeatherService.Result) -> Void) { dataSource.fetchWeatherReport(latitude: latitude, longitude: longitude, completion: completion) } }
  13. struct WeatherService { enum Result { case success(WeatherReport) case failure(WeatherService.Error)

    // Many would use ErrorType } enum Error { case invalidLatitude case invalidLongitude case parseError case noPreparedResponse case networkError(NSError) case noDataError } }
  14. enum ValidatorError: Equatable { case MinLengthInvalid(actual: Int, required: Int) case

    MaxLengthInvalid(actual: Int, allowed: Int) case RequiredCharacterMissing(requiredSet: NSCharacterSet) case RequiredPrefixCharacterMissing(requiredPrefixSet: NSCharacterSet) case DoesNotMatchRegex(regex: String) } // Taken From Swift2 project, forgive the CamelCase enum names.
  15. struct WeatherService { enum Result { case success(WeatherReport) case failure(WeatherService.Error)

    } enum Error {} func fetchWeatherReport(latitude: Double, longitude: Double, completion: @escaping (_ result: WeatherService.Result) -> Void) { dataSource.fetchWeatherReport(latitude: latitude, longitude: longitude, completion: completion) } }
  16. private extension UIColor { convenience init(hex: UInt32) { // Note:

    This is called the bit shift operator let red = CGFloat((hex >> 16) & 0xff) / 255 let green = CGFloat((hex >> 8) & 0xff) / 255 let blue = CGFloat((hex ) & 0xff) / 255 self.init(red: red, green: green, blue: blue, alpha: 1) } } let yellow = UIColor(hex: 0xECB02F) // Note: These are called hexadecimal literals
  17. private enum BNRColors { static let offWhite = UIColor(hex: 0xEEEEEE)

    static let offBlack = UIColor(hex: 0x333333) static let red = UIColor(hex: 0xE15827) static let yellow = UIColor(hex: 0xECB02F) }
  18. enum Theme: String { case day = "com.ranchweather.theme.day" case night

    = "com.ranchweather.theme.night" var backgroundColor: UIColor { switch self { case .day: return BNRColors.offWhite case .night: return BNRColors.offBlack } } var textColor: UIColor { switch self { case .day: return BNRColors.offBlack case .night: return BNRColors.offWhite } } var tintColor: UIColor { switch self { case .day: return BNRColors.red case .night: return BNRColors.yellow } } var preferredStatusBarStyle: UIStatusBarStyle { switch self { case .day: return UIStatusBarStyle.default case .night: return UIStatusBarStyle.lightContent } } }
  19. struct Themer { let theme: Theme init(theme: Theme) { self.theme

    = theme updateGlobalThemeSettings() } func updateGlobalThemeSettings() { updateNavigationBar() reloadWindow() } func updateNavigationBar() { let apperance = UINavigationBar.appearance() apperance.barTintColor = theme.backgroundColor apperance.titleTextAttributes = [NSForegroundColorAttributeName: theme.textColor] apperance.tintColor = theme.tintColor }
  20. struct Themer { ... // Manual calls func theme(_ tableView:

    UITableView) { tableView.backgroundColor = theme.tableViewBackgroundColor } func theme(_ cell: UITableViewCell) { cell.backgroundColor = theme.tableViewCellBackgroundColor cell.textLabel?.textColor = theme.tableViewCellTextColor } func theme(_ view: UIView) { view.backgroundColor = theme.tableViewBackgroundColor } }
  21. x4