Slide 1

Slide 1 text

.BLJOH:PVS "QQ4UBUJD :VTFJ/JTIJZBNB !ZVTFJOJTIJZBNB QPUBUPUJQT

Slide 2

Slide 2 text

8IPBN* w :VTFJ/JTIJZBNB !ZVTFJOJTIJZBNB w J04EFWFMPQFSZFBST w $PPLQBE w *OUFSOBUJPOBMHSPVQ w -FBSOJOH&OHMJTI

Slide 3

Slide 3 text

(MPCBM

Slide 4

Slide 4 text

/PXXFIBWFB TUBUJDMBOHVBHFʜ

Slide 5

Slide 5 text

5IFSFTUJMM SFNBJOTBMPUPG EZOBNJDQBSU

Slide 6

Slide 6 text

6*7JFX$POUSPMMFS

Slide 7

Slide 7 text

let storyBoard = UIStoryboard(name: "Main", bundle: nil) let vc = storyBoard.instantiateViewControllerWithIdentifier("FooBar") as! FooBarViewController navigationController?.pushViewController(vc, animated: true) class FooBarViewController : UIViewController {}

Slide 8

Slide 8 text

$PEF(FOFSBUPS wLS[Z[BOPXTLJN/BUBMJF wTRVBSFPCKDDPEFHFOVUJMT wQBVMTBNVFMT4#$POTUBOUT

Slide 9

Slide 9 text

4UPSZCPBSE
 7JFX$POUSPMMFS w $MBTTOBNF'JMFOBNF 4UPSZCPBSE ‣ 'PP#BS7JFX$POUSPMMFSTXJGU ‣ 'PP#BS7JFX$POUSPMMFSTUPSZCPBSE w 4UPSZCPBSE&OUSZ1PJOU class FooBarViewController : UIViewController { var aProperty: Int?

Slide 10

Slide 10 text

protocol StoryboardInstantiable {} func instantiate(_: 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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

)PXUPHFU BUZQFOBNF

Slide 14

Slide 14 text

0CKFDUJWF$ NSString * className = NSStringFromClass([FooBarViewController class]); NSLog(@"Class name is %@", className);

Slide 15

Slide 15 text

4XJGU let classString = NSStringFromClass(FooBarViewController) // => Potatotips.FooBarViewController classString.componentsSeparatedByString(".").last! // => FooBarViewController

Slide 16

Slide 16 text

/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

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

.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

Slide 19

Slide 19 text

%FNBOHMFE5ZQF/BNF println(reflect(Hoge.FooBarViewController.self).summary) // => Potatotips.Hoge.FooBarViewController println(toString(Hoge.FooBarViewController.self)) // => Potatotips.Hoge.FooBarViewController

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

4XJGU print(String(Hoge.FooBarViewController.self)) // => Potatotips.Hoge.FooBarViewController

Slide 22

Slide 22 text

6*5BCMF7JFX

Slide 23

Slide 23 text

override func tableView( tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier( "Cell", forIndexPath: indexPath) as! FooBarCell /4*OEFY1BUI

Slide 24

Slide 24 text

NSIndexPath Meaning

Slide 25

Slide 25 text

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) } } }

Slide 26

Slide 26 text

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" } // ...

Slide 27

Slide 27 text

6*5BCMF7JFX$FMM override func tableView( tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier( "Cell", forIndexPath: indexPath) as! FooBarCell

Slide 28

Slide 28 text

$MBTTOBNF
 'JMFOBNF OJC 
 *EFOUJpFS

Slide 29

Slide 29 text

extension UITableView { func registerNibForCellWithType(type: T.Type) { let className = TypeNameFromType(T) let nib = UINib(nibName: className, bundle: nil) registerNib(nib, forCellReuseIdentifier: className) } func registerClassForCellWithType(type: T.Type) { let className = TypeNameFromType(T) registerClass(T.self, forCellReuseIdentifier: className) } func dequeueReusableCellWithType( type: T.Type, forIndexPath indexPath: NSIndexPath) -> T { return dequeueReusableCellWithIdentifier( TypeNameFromType(T), forIndexPath: indexPath) as! T } } 3FHJTUFSBOE %FRVFVFCZ5ZQF

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

4VNNBSZ

Slide 32

Slide 32 text

w'JOETPNFUIJOHXSPOHPO lDPNQJMFUJNFz w(FOFSBUJOHTUSJOHTGSPN UZQFT w3FEVDFDPEFXJUIHFOFSJDT

Slide 33

Slide 33 text

8FBSFIJSJOH

Slide 34

Slide 34 text

"OZ2VFTUJPOT