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

Alexander Zimin -MVVM в Swift

Alexander Zimin -MVVM в Swift

CocoaHeads

April 29, 2016
Tweet

More Decks by CocoaHeads

Other Decks in Programming

Transcript

  1. • Про работу с базовыми инструментами (UICollectionView, Swift и прочие)

    - 26% • Про подходы к разработке крупных проектов (MVVM, CI и прочие) - 37%
  2. MVC struct Person { var firstName: String var secondName: String

    }s class ViewController: UIViewController { var person: Person! @IBOutlet var nameLabel: UILabel! }s
  3. MVC struct Person { var firstName: String var secondName: String

    }s class ViewController: UIViewController { var person: Person! @IBOutlet var nameLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() nameLabel.text = "\(person.firstName) \(person.secondName)" } }s
  4. MVVM struct Person { var firstName: String var secondName: String

    }s struct PersonModel { var person: Person var name: String { return "\(person.firstName) \(person.secondName)" } } class ViewController: UIViewController { var person: Person! @IBOutlet var nameLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() nameLabel.text = "\(person.firstName) \(person.secondName)" } }s
  5. MVVM struct Person { var firstName: String var secondName: String

    }s struct PersonModel { var person: Person var name: String { return "\(person.firstName) \(person.secondName)" } } class ViewController: UIViewController { var personModel: PersonModel! @IBOutlet var nameLabel: UILabel! override func viewDidLoad() { super.viewDidLoad() nameLabel.text = personModel.name } }s
  6. MVVM Схема ViewModel Model Уведомляет Обновляет Controller View View Уведомляет

    Воздействует • Обновляет: • gameSession.attempts += 1 • Воздействует: • gameSessionEntity.loseAction()
  7. MVVM Уведомляет • KVO: • Плохо (никаких enum или struct)

    • Functional reactive programming: • ReactiveCocoa • RxSwift
  8. MVVM Уведомляет • KVO • Плохо (никаких enum или struct)

    • Functional reactive programming: • ReactiveCocoa • RxSwift http://stackoverflow.com/questions/32542846/reactivecocoa-vs-rxswift-pros-and-cons https://www.raywenderlich.com/126522/reactivecocoa-vs-rxswift
  9. MVVM RxSwift var name = Variable("Value") let subscribe = name.asObservable().subscribeNext

    { (value) in print(value) }s name.value = "New Value" subscribe.dispose() name.value = "Another Value" Value New Value
  10. Swift Не Objective-C • Вы что-то делаете не так если:

    • Наследуете NSObject • Частое используете class • Слово protocol и Delegate всегда на одной строчке • Последний раз видели T в C#
  11. Swift Не Objective-C • В какую сторону смотреть • POP

    (Protocol-Oriented Programming) • Protocols, Value Type • Functional reactive programming • Generic • State machine • Enums
  12. Swift POP в Swift • В начале любой иерархии протокол

    • Приоритетное использование Value Type (struct, enum)
  13. protocol ColletionItem { var id: String { get } var

    name: String { get } }s Swift POP в Swift
  14. protocol ColletionItem { var id: String { get } var

    name: String { get } }s struct SongItem: ColletionItem { let id: String private(set) var name: String // ... init(id: String, name: String) { self.id = id self.name = name } } Swift POP в Swift
  15. protocol ColletionItem { var id: String { get } var

    name: String { get } }s extension ColletionItem { var title: String { if name.characters.count > 0 { return name } return "Uncnown" } } Swift POP в Swift
  16. protocol TextFormatType { var textFont: UIFont { get } var

    textColor: UIColor { get } }s Swift Component-Based Design
  17. protocol TextFormatType { var textFont: UIFont { get } var

    textColor: UIColor { get } }s extension TextFormatType { var textFont: UIFont { return .systemFontOfSize(12) } var textColor: UIColor { return .blackColor() } } Swift Component-Based Design
  18. protocol ViewFormatType { var backgroundColor: UIColor { get } var

    cornerRadius: CGFloat { get } }s Swift Component-Based Design
  19. protocol ViewFormatType { var backgroundColor: UIColor { get } var

    cornerRadius: CGFloat { get } }s extension ViewFormatType { var backgroundColor: UIColor { return .whiteColor() } var cornerRadius: CGFloat { return 0 } } Swift Component-Based Design
  20. protocol TitleLabelAppearanceType: ViewFormatType, TextFormatType { } extension TitleLabelAppearanceType { var

    textFont: UIFont { return .boldSystemFontOfSize(16) } } Swift Component-Based Design
  21. • https://medium.com/ios-os-x-development/ios-architecture- patterns-ecba4c38de52 - Паттерны в iOS • http://artsy.github.io/blog/2015/09/24/mvvm-in-swift/ -

    Проблемы MVVM в Swift • https://www.captechconsulting.com/blogs/ios-9-tutorial- series-protocol-oriented-programming-with-uikit - Хороший пример использования POP • https://github.com/ReactKit/SwiftState - Машина состояний в Swift Полезные ссылки