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

Going Swift and Beyond // First Wave Swift

Ayaka Nonaka
January 29, 2016

Going Swift and Beyond // First Wave Swift

Talk given 29 Jan 2016 at dotSwift Paris

Swift is a language that brings a lot of great new features like optionals, generics, and first-class enums. How do these new features affect the way we architect our apps? Do the same design patterns from Objective-C still apply in Swift? We’ll go over a specific example of how Swift affects what kind of code we write, how we write our code, and even a bit of why we do what we do.

Ayaka Nonaka

January 29, 2016
Tweet

More Decks by Ayaka Nonaka

Other Decks in Technology

Transcript

  1. struct Story { let ID: String let title: String let

    message: String let sender: User let recipient: User let date: NSDate // ... }
  2. class StoryDetailViewController: UIViewController { private let titleView: StoryTitleView private let

    senderView: AvatarView private let recipientView: AvatarView private let dateLabel: DateLabel init(story: Story) { titleView = StoryTitleView(story: story) senderView = AvatarView(user: story.sender) recipientView = AvatarView(user: story.recipient) dateLabel = DateLabel(date: story.date) } // ... }
  3. class StoryDetailViewController: UIViewController { private let titleView: StoryTitleView private let

    senderView: AvatarView private let recipientView: AvatarView private let dateLabel: DateLabel init(story: Story) { /* Same as before */ } init(storyID: String) { // Hmmmmmmm. } }
  4. class StoryDetailViewController: UIViewController { let storyID: String private var titleView:

    StoryTitleView? private var senderView: AvatarView? private var recipientView: AvatarView? private var dateLabel: DateLabel? init(story: Story) { /* Same as before */ } init(storyID: String) { self.storyID = storyID titleView = nil senderView = nil recipientView = nil dataLabel = nil } // Load everything from API in viewDidLoad? }
  5. class StoryContainerViewController: UIViewController { let storyID: String init(storyID: String) {

    self.storyID = storyID } override func viewDidLoad() { client.showStory(ID: storyID) { result in switch result { case .Success(let story): let viewController = StoryDetailViewController(story: story) self.addChildViewController(viewController) self.view.addSubview(viewController.view) viewController.view.frame = view.bounds viewController.didMoveToParentViewController(self) case .Error(let error): // Show error } } } }
  6. protocol RemoteContentCoordinator { typealias Content func fetchContent(completion: Result<Content, Error> ->

    Void) func viewControllerForContent(content: Result<Content, Error>) -> UIViewController }
  7. class RemoteContentContainerViewController<T: RemoteContentCoordinator>: UIViewController { let coordinator: T init(coordinator: T)

    { self.remoteContentCoordinator = remoteContentCoordinator super.init(nibName: nil, bundle: nil) } override func viewDidLoad() { super.viewDidLoad() coordinator.fetchContent { content in let viewController = self.coordinator.viewControllerForContent(content) self.addChildViewController(viewController) self.view.addSubview(viewController.view) viewController.view.frame = view.bounds viewController.didMoveToParentViewController(self) } } }
  8. struct StoryCoordinator: RemoteContentCoordinator { let ID: String func fetchContent(completion: Result<Story,

    Error> -> Void) { client.showStory(ID: ID, completion: completion) } func viewControllerForContent(content: Result<Story, Error>) -> UIViewController { switch content { case .Success(let story): return StoryDetailViewController(story: story) case .Error(_): return ErrorViewController(title: "Could not find story.") } } }
  9. class StoryDetailViewController: UIViewController { let storyID: String private var titleView:

    StoryTitleView? private var senderView: AvatarView? private var recipientView: AvatarView? private var dateLabel: DateLabel? init(story: Story) { /* Same as before */ } init(storyID: String) { self.storyID = storyID titleView = nil senderView = nil recipientView = nil dataLabel = nil } // Load everything from API in viewDidLoad? }
  10. class StoryDetailViewController: UIViewController { private let titleView: StoryTitleView private let

    senderView: AvatarView private let recipientView: AvatarView private let dateLabel: DateLabel init(story: Story) { titleView = StoryTitleView(story: story) senderView = AvatarView(user: story.sender) recipientView = AvatarView(user: story.recipient) dateLabel = DateLabel(date: story.date) } // ... }
  11. HOW

  12. WHY

  13. TRY

  14. TRY > Fix a bug in backend or web code

    > Learn a different language > Talk about architecture with an Android(!) engineer
  15. INCLUDE > Get them excited about Swift > Be open

    to unfamiliar ideas > Be welcoming