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

ITT 2016 - Michele Titolo - Cocoa Design Patterns in Swift

ITT 2016 - Michele Titolo - Cocoa Design Patterns in Swift

Michele Titolo talks about Cocoa Design Patterns in Swift. Swift has now been around for almost two years, and the language has evolved. What hasn't changed are the APIs in Apple's SDK you work with every day. As much as you want to embrace Swift, you still must understand the SDK conventions, many of which originated in the Cocoa era. Continuing to write Swift like Objective-C is not ideal or sustainable. Michele Titolo focuses on the existing patterns and how to make them more friendly in Swift.

Istanbul Tech Talks

April 05, 2016
Tweet

More Decks by Istanbul Tech Talks

Other Decks in Programming

Transcript

  1. AbsoluteValuable, AnyCollectionType, AnyObject, ArrayLiteralConvertible, BidirectionalIndexType, BitwiseOperationsType, BooleanLiteralConvertible, BooleanType, CVarArgType, CollectionType,

    Comparable, CustomDebugStringConvertible, CustomLeafReflectable, CustomPlaygroundQuickLookable, CustomReflectable, CustomStringConvertible, DictionaryLiteralConvertible, Equatable, ErrorType, ExtendedGraphemeClusterLiteralConvertible, FloatLiteralConvertible, FloatingPointType, ForwardIndexType, GeneratorType, Hashable, Indexable, IntegerArithmeticType, IntegerLiteralConvertible, IntegerType, IntervalType, LazyCollectionType, LazySequenceType, MirrorPathType, MutableCollectionType, MutableIndexable, MutableSliceable, NilLiteralConvertible, OptionSetType, OutputStreamType, RandomAccessIndexType, RangeReplaceableCollectionType, RawRepresentable, ReverseIndexType, SequenceType, SetAlgebraType, SignedIntegerType, SignedNumberType, Streamable, Strideable, StringInterpolationConvertible, StringLiteralConvertible, UnicodeCodecType, UnicodeScalarLiteralConvertible, UnsignedIntegerType @gregheo - Swift Summit SF 2015
  2. protocol Holder { typealias Item var items: [Item] { get

    set } } extension Holder { mutating func add(item: Item) { items.append(item) } }
  3. class MyTableViewController: UITableViewController { override func viewDidLoad() { super.viewDidLoad() if

    traitCollection.forceTouchCapability == .Available { registerForPreviewingWithDelegate(self, sourceView: tableView) } } } // MARK: Force Touch on Table Cell extension MyTableViewController: UIViewControllerPreviewingDelegate { … }
  4. class Basket { var items = [AnyObject]() init(items: [AnyObject]) {

    self.items.appendContentsOf(items) } } class FruitBasket: Basket { init<T: AnyObject where T: Fruit>(items: [T]) { super.init(items: items) } }
  5. protocol FormRowViewModel { var title: String { get } var

    placeholder: String { get } func validationFunction() -> String -> Bool } class FormTableView<T: FormRowViewModel> UITableView { var rows = [T]() }
  6. let table = FormTableView(frame: CGRectMake(0, 0, 100, 100), style: .Plain)

    let person = PeopleRowViewModel() table.rows.append(person) let bear = BearRowViewModel() table.rows.append(bear) !
  7. let table = FormTableView<PeopleRowViewModel>(frame: CGRectMake(0, 0, 100, 100), style: .Plain)

    let person = PeopleRowViewModel() table.rows.append(person) let bear = BearRowViewModel() table.rows.append(bear)
  8. - (NSInteger)figureOutSomeState { if ([self.someOtherObject.someString isEqual:@"Hello"] && self.someFlag) { return

    1; } else if (self.someOtherFlag && !self.someFlag) { return 2; } else if (self.someOtherFlag && self.someFlag && [self.someOtherObject.someString isEqual:@"Goodbye"]) { return 3; } return 0; }
  9. lazy var someState: Int = { [unowned self] in if

    self.someOtherObject.title == "Hello" && self.someFlag { return 1 } else if self.someOtherFlag && !self.someFlag { return 2 } else if self.someOtherFlag && self.someFlag && self.someOtherObject.title == "Goodbye" { return 3 } return 0 }()
  10. var refreshing = false { willSet(newRefreshing) { if (newRefreshing) {

    self.tableView.tableHeaderView = self.loadingView } else { self.tableView.tableHeaderView = nil } } }
  11. func decode(json: JSON) -> User? { return _JSONObject(json) >>> {

    d in User.create <^> d["id"] >>> _JSONInt <*> d["name"] >>> _JSONString <*> d["email"] >>> _JSONString } }
  12. Photo Credits • https://www.flickr.com/photos/19779889@N00/26092186311/ • https://www.flickr.com/photos/55602651@N08/11338995214/ • https://www.flickr.com/photos/23813062@N08/6912401481/ • https://www.flickr.com/photos/92485952@N02/20167637213/

    • https://www.flickr.com/photos/84047449@N00/21687886133 • https://www.flickr.com/photos/50318388@N00/2392118348 • https://www.flickr.com/photos/129231629@N05/20890434523/ • https://www.flickr.com/photos/39375632@N05/4604343140/ • https://www.flickr.com/photos/13696909@N00/23545354390/ • https://www.flickr.com/photos/132691994@N07/23756257432