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

Making Your App Static

Making Your App Static

Making Your App Static with Swift

Yusei Nishiyama

July 14, 2015
Tweet

More Decks by Yusei Nishiyama

Other Decks in Technology

Transcript

  1. let storyBoard = UIStoryboard(name: "Main", bundle: nil) let vc =

    storyBoard.instantiateViewControllerWithIdentifier("FooBar") as! FooBarViewController navigationController?.pushViewController(vc, animated: true) class FooBarViewController : UIViewController {}
  2. 4UPSZCPBSE
 7JFX$POUSPMMFS w $MBTTOBNF'JMFOBNF 4UPSZCPBSE  ‣ 'PP#BS7JFX$POUSPMMFSTXJGU ‣ 'PP#BS7JFX$POUSPMMFSTUPSZCPBSE

    w 4UPSZCPBSE&OUSZ1PJOU class FooBarViewController : UIViewController { var aProperty: Int?
  3. protocol StoryboardInstantiable {} func instantiate<T: StoryboardInstantiable>(_: T.Type) -> T {

    let storyBoard = UIStoryboard(name: TypeNameFromType(T), bundle: nil) return storyBoard.instantiateInitialViewController() as! T } extension FooBarViewController : StoryboardInstantiable {} let vc = instantiate(FooBarViewController) vc.aProperty = 1 navigationController?.pushViewController(vc, animated: true) 1SPUPDPM (MPCBM'VODUJPO
  4. 1SPUPDPM&YUFOTJPO protocol StoryboardInstantiable {} extension StoryboardInstantiable { static func instantiate()

    -> Self { let storyBoard = UIStoryboard(name: TypeNameFromType(Self), bundle: nil) return storyBoard.instantiateInitialViewController() as! Self } } extension FooBarViewController : StoryboardInstantiable {} let vc = FooBarViewController.instantiate() vc.aProperty = 1 4XJGU
  5. 5ZQF$POTUSBJOUT extension StoryboardInstantiable where Self: UIViewController { static func instantiate()

    -> Self { let storyBoard = UIStoryboard(name: TypeNameFromType(Self), bundle: nil) return storyBoard.instantiateInitialViewController() as! Self } } class SomeClass : StoryboardInstantiable {} SomeClass.instantiate() // Compile error
  6. /FTUFE$MBTT class Hoge { class FooBarViewController : UIViewController {} }

    println(Hoge.FooBarViewController.self) // => Potatotips.Hoge.FooBarViewController println(NSStringFromClass(Hoge.FooBarViewController.self)) // => _TtCC10Potatotips4Hoge20FooBarViewController println(Hoge.FooBarViewController.self.description()) // => _TtCC10Potatotips4Hoge20FooBarViewController println(Hoge.FooBarViewController.self.debugDescription()) // => _TtCC10Potatotips4Hoge20FooBarViewController
  7. .BOHMFE/BNF $ xcrun swift-demangle _TtCC10Potatotips4Hoge20FooBarViewController
 _TtCC10Potatotips4Hoge20FooBarViewController ---> Potatotips.Hoge.FooBarViewController _T t

    CC 10Potatotips 4Hoge 20FoobarViewController Swift global symbol Type 2 nested class Module name
 (10 characters) Class name Class name
  8. TUSVDUPSFOVN struct A { struct B { struct C {}

    } } enum X { enum Y {} } println(reflect(A.B.C.self).summary) // => Potatotips.A.B.C println(reflect(X.Y.self).summary) // => Potatotips.X.Y println(toString(A.B.C.self)) // => Potatotips.A.B.C println(toString(X.Y.self)) // => Potatotips.X.Y
  9. override func tableView( tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

    { let cell = tableView.dequeueReusableCellWithIdentifier( "Cell", forIndexPath: indexPath) as! FooBarCell /4*OEFY1BUI
  10. enum Section { case Birds(row: Bird), Fishes(row: Fish) enum Bird:

    Int { case Parrot, Owl, Woodpeckers } enum Fish: Int { case Carp, Dragonfish } init?(indexPath: NSIndexPath) { switch (indexPath.section, indexPath.row) { case (0, let x): guard let bird = Bird(rawValue: x) else { return nil } self = Birds(row: bird) case (1, let x): guard let fish = Fish(rawValue: x) else { return nil } self = Fishes(row: fish) default: return nil } } var indexPath: NSIndexPath { switch self { case .Birds(let row): return NSIndexPath(forRow: row.rawValue, inSection: 0) case .Fishes(let row): return NSIndexPath(forRow: row.rawValue, inSection: 1) } } }
  11. override func tableView( tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell

    { let title: String if let section = Section(indexPath: indexPath) { switch section { case .Birds(row: .Owl): title = "Owl" default: title = "Other" } } else { title = "Invalid" } // ...
  12. 6*5BCMF7JFX$FMM override func tableView( tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) ->

    UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier( "Cell", forIndexPath: indexPath) as! FooBarCell
  13. extension UITableView { func registerNibForCellWithType<T: UITableViewCell>(type: T.Type) { let className

    = TypeNameFromType(T) let nib = UINib(nibName: className, bundle: nil) registerNib(nib, forCellReuseIdentifier: className) } func registerClassForCellWithType<T: UITableViewCell>(type: T.Type) { let className = TypeNameFromType(T) registerClass(T.self, forCellReuseIdentifier: className) } func dequeueReusableCellWithType<T: UITableViewCell>( type: T.Type, forIndexPath indexPath: NSIndexPath) -> T { return dequeueReusableCellWithIdentifier( TypeNameFromType(T), forIndexPath: indexPath) as! T } } 3FHJTUFSBOE %FRVFVFCZ5ZQF
  14. class FooBarCell : UITableViewCell { @IBOutlet weak var label: UILabel!

    } // Register let tableView = UITableView() tableView.registerNibForCellWithType(FooBarCell) // Dequeue let cell = tableView.dequeueReusableCellWithType( FooBarCell.self, forIndexPath: indexPath) cell.label.text = title 1VUUIFNJOUP "DUJPO