Slide 1

Slide 1 text

ʮจԽΛௐ࿨ͤ͞Δʯ Λ΍ͬͯΈΔ 5SZl#MFOEJOH$VMUVSFz

Slide 2

Slide 2 text

·ͣ࠷ॳʹʜɻ

Slide 3

Slide 3 text

ுΓ੾Γ͗͢·ͨ͠ ݁ߏͳεϥΠυ਺Ͱ͢ ςϯϙΑ͘ਐΊ͍͖ͯ·͢ʂʂ

Slide 4

Slide 4 text

ʮจԽΛௐ࿨ͤ͞Δʯͱ͸ʁ w USZ 4XJGUͷ̍೔໨ʹߦΘΕͨ %BOJFM)4UFJOCFSHࢯͷൃද ʮ#MFOEJOH$VMUVSFʯͷ͜ͱ w 4XJGUΒ͍͠.7$Ϟσϧͷ 5BCMF7JFX$POUSPMMFSͷ࡞Γ ํΛ঺հ͍ͯͨ͠ w USZ4XJGU͔ͩΒɺ5SZ l#MFOEJOH$VMUVSFzͯ͜ͱ Ͱɺ౰೔Λࢥ͍ग़ͯࣗ͠෼Ͱ ΍ͬͯΈΔ Ҿ༻ݩɿIUUQEFWDMBTTNFUIPEKQTNBSUQIPOFUSZTXJGUTUEBZQN

Slide 5

Slide 5 text

– Daniel H Steinberg l4XJGUͰΞϓϦέʔγϣϯΛॻ͘ͱ͍͏͜ͱ͸0CKFDUJWF$ Ͱॻ͔ΕͨΞϓϦέʔγϣϯΛͨͩ4XJGUʹॻ͖׵͑Δ͚ͩ Ͱͳ͘ɺ4XJGUͷಛ௃΍఩ֶΛड͚ೖΕΔඞཁ͕͋Γ·͢ɻ ͜ͷߨԋͰ͸ɺඪ४తͳ.7$Ͱߏ੒͞ΕͨςʔϒϧϏϡʔ Λ࢖༻ͨ͠ΞϓϦέʔγϣϯΛ4XJGUʹॻ͖׵͑Δͱ͜Ζ͔ Β࢝·ΓɺΑΓ4XJGUΒ͍͠ίʔυʹ͢ΔͨΊʹɺؔ਺ܕϓ ϩάϥϛϯάɺΦϒδΣΫτࢦ޲ϓϩάϥϛϯάɺσβΠϯ ύλʔϯɺϓϩτίϧࢦ޲ϓϩάϥϛϯάͷߟ͑ํΛద༻͠ ͍͖ͯ·͢ɻz ʮจԽΛௐ࿨ͤ͞Δʯͱ͸ʁ

Slide 6

Slide 6 text

ௐ࿨͍ͤͨ͞จԽͨͪ w 0010CKFDU0SJFOUFE1SPHSBNNJOH ΦϒδΣΫτ ࢦ޲ϓϩάϥϛϯά w 1011SPUPDPM0SJFOUFE1SPHSBNNJOH ϓϩτίϧࢦ ޲ϓϩάϥϛϯά w '1'VODUJPOBM1SPHSBNNJOH ϑΝϯΫγϣφϧϓϩ άϥϛϯά w .7$.PEFM7JFX$POUSPMMFS ϞσϧɾϏϡʔɾίϯ τϩʔϥʔ

Slide 7

Slide 7 text

#MFOEJOH$VMUVSF $POUSPMMFS .PEFM 7JFX 001 101 '1

Slide 8

Slide 8 text

#MFOEJOH$VMUVSF4BNQMF $POUSPMMFS .PEFM 7JFX

Slide 9

Slide 9 text

$POUSPMMFS 7JFX )BOE #MFOEJOH$VMUVSF4BNQMF

Slide 10

Slide 10 text

$POUSPMMFS )BOE .BJO TUPSZCPBSE #MFOEJOH$VMUVSF4BNQMF

Slide 11

Slide 11 text

)BOE )BOE7$ .BJO TUPSZCPBSE #MFOEJOH$VMUVSF4BNQMF

Slide 12

Slide 12 text

'JSTU%FNP w USZ4XJGU౰೔ͷൃදͰ͸ %FDL $BSEΫϥεͷιʔε͸ ެ։͞Εͣ w ಠࣗͰߟ࣮͑૷ͯ͠Έͨਓ͕ 2JJUBʹ౤ߘ͍ͯͨ͠ͷͰࢀߟ ʹͨ͠ w IUUQRJJUBDPNTV@LJUFNT DBFGBBGF

Slide 13

Slide 13 text

)BOE7$TXJGU खࡳͷ௥Ճมߋલ import UIKit class HandVC: UITableViewController { private let hand = Hand() override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { if hand.numberOfCards < 5 { hand.addNewCardAtIndex(0) tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } }

Slide 14

Slide 14 text

)BOE7$TXJGU खࡳͷ௥Ճมߋޙ import UIKit class HandVC: UITableViewController { private let hand = Hand() override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { if hand.numberOfCards < 5 { hand.addNewCardAtIndex(0) insertTopRow() } } private func insertTopRow() { tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) }

Slide 15

Slide 15 text

)BOE7$TXJGU खࡳͷຕ਺ͱදࣔ // MARK: - Table view data source override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return hand.numberOfCards } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier(“cardCell", forIndexPath: indexPath) let card = hand.cardAtPosition(indexPath.row) cell.textLabel?.text = card.rank.description cell.textLabel?.textColor = card.color cell.detailTextLabel?.text = card.suit.description return cell }

Slide 16

Slide 16 text

)BOE7$TXJGU खࡳͷ࡟আมߋલ override func tableView(tableView: UITableView, commitEditingStyle editingStyle:UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { hand.deleteCardAtIndex(indexPath.row) tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } }

Slide 17

Slide 17 text

)BOE7$TXJGU खࡳͷ࡟আมߋޙ override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { hand.deleteCardAtIndex(indexPath.row) deleteRowAtIndexPath(indexPath) } } private func deleteRowAtIndexPath(indexPath: NSIndexPath) { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) }

Slide 18

Slide 18 text

)BOE7$TXJGU खࡳͷҠಈ override func tableView(tableView: UITableView moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { hand.moveCard(sourceIndexPath.row, toIndex: destinationIndexPath.row) }

Slide 19

Slide 19 text

)BOETXJGU खࡳͷϞσϧΫϥε import UIKit class Hand { private let deck = Deck() private var cards = [Card]() var numberOfCards: Int { return cards.count } func cardAtPosition(index: Int) -> Card { return cards[index] }

Slide 20

Slide 20 text

)BOETXJGU खࡳͷϞσϧΫϥε௥Ճɾ࡟আɾҠಈ func addNewCardAtIndex(index: Int) { insertCard(deck.nextCard(), atIndex: index) } private func insertCard(card: Card, atIndex index: Int) { cards.insert(card, atIndex: index) } func deleteCardAtIndex(index: Int) { cards.removeAtIndex(index) } func moveCard(fromIndex: Int, toIndex: Int) { let cardToMove = cards[fromIndex] deleteCardAtIndex(fromIndex) insertCard(cardToMove, atIndex: toIndex) }

Slide 21

Slide 21 text

)BOETXJGU खࡳͷϞσϧΫϥε௥Ճɾ࡟আɾҠಈ func addNewCardAtIndex(index: Int) { insertCard(deck.nextCard(), atIndex: index) } private func insertCard(card: Card, atIndex index: Int) { cards.insert(card, atIndex: index) } func deleteCardAtIndex(index: Int) { cards.removeAtIndex(index) } func moveCard(fromIndex: Int, toIndex: Int) { let cardToMove = cards[fromIndex] deleteCardAtIndex(fromIndex) insertCard(cardToMove, atIndex: toIndex) }

Slide 22

Slide 22 text

)BOE )BOE7$ .BJO TUPSZCPBSE #MFOEJOH$VMUVSF4BNQMF

Slide 23

Slide 23 text

– Daniel H Steinberg l͜͜·Ͱ͸ೃછΈ͕͋Γ·͔͢ʁ͜ΕͰຬ଍Ͱ͔͢ʁ ͔͜͜Β෼ͰͲ͏ల։Ͱ͖Δ͔ͷ͓࿩Λ͢Δɻ ͜Ε͕࠷ળͱ͸ݴΘͳ͍͕ɺ͜Μͳͷ͸೗Կͱ͍͏͜ͱͰz ͔͜͜ΒɺจԽΛௐ࿨ͤ͞Δ

Slide 24

Slide 24 text

$MBTTUPB4USVDU

Slide 25

Slide 25 text

)BOETXJGU खࡳͷϞσϧߏ଄ମ import UIKit struct Hand { private let deck = Deck() private var cards = [Card]() var numberOfCards: Int { return cards.count } func cardAtPosition(index: Int) -> Card { return cards[index] }

Slide 26

Slide 26 text

4XJGUͷDMBTTͱTUSVDUͷҧ͍ w l.PEFM$MBTTͰ͸ͳ͘ 7BMVF5ZQFʹ͠·͢z%BOJFM )4UFJOCFSH w খͯ͘͞୯७ͳσʔλΛΧϓ ηϧԽ͍ͨ͠৔߹ w ࢀর౉͠ΑΓ஋౉͠ ίϐʔ ͕ ͍͍৔߹ w ܧঝ͕ඞཁͳ͍৔߹ http://qiita.com/mini_house/items/22b7b88794b9f8a32381

Slide 27

Slide 27 text

ߏ଄ମͷ஋Λॻ͖׵͑Δ ؔ਺ʹ͸ɺNVUBUJOH͕ඞཁ

Slide 28

Slide 28 text

)BOETXJGU खࡳͷϞσϧߏ଄ମ௥Ճɾ࡟আɾҠಈ mutating func addNewCardAtIndex(index: Int) { insertCard(deck.nextCard(), atIndex: index) } private mutating func insertCard(card: Card, atIndex index: Int) { cards.insert(card, atIndex: index) } mutating func deleteCardAtIndex(index: Int) { cards.removeAtIndex(index) } mutating func moveCard(fromIndex: Int, toIndex: Int) { let cardToMove = cards[fromIndex] deleteCardAtIndex(fromIndex) insertCard(cardToMove, atIndex: toIndex) }

Slide 29

Slide 29 text

)BOE7$TXJGU ϞσϧͷએݴΛఆ਺͔Βม਺΁ import UIKit class HandVC: UITableViewController { private var hand = Hand() override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { if hand.numberOfCards < 5 { hand.addNewCardAtIndex(0) tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } }

Slide 30

Slide 30 text

NVUBUJOH

Slide 31

Slide 31 text

-FU`TFMJNJOBUFNVUBUJOH

Slide 32

Slide 32 text

)BOETXJGU खࡳͷߏ଄ମ௥ՃͷNVUBUJOHΛ֎͢ mutating func addNewCardAtIndex(index: Int) { insertCard(deck.nextCard(), atIndex: index) } func addNewCardAtIndex(index: Int) -> Hand { return insertCard(deck.nextCard(), atIndex: index) }

Slide 33

Slide 33 text

)BOETXJGU खࡳͷߏ଄ମ௥ՃͰΠϯελϯεΛฦ͢ private func insertCard(card: Card, atIndex index: Int) -> Hand { var mutableCards = cards mutableCards.insert(card, atIndex: index) return Hand(deck: deck, cards: mutableCards) }

Slide 34

Slide 34 text

)BOETXJGU खࡳͷߏ଄ମ௥ՃͰΠϯελϯεΛฦ͢ private func insertCard(card: Card, atIndex index: Int) -> Hand { var mutableCards = cards mutableCards.insert(card, atIndex: index) return Hand(deck: deck, cards: mutableCards) }

Slide 35

Slide 35 text

4XJGUͷߏ଄ମ͸ϓϩύςΟΛ Ҿ਺ʹ΋ͭΠχγϟϥΠβ͕ ࣗಈੜ੒͞ΕΔ

Slide 36

Slide 36 text

)BOETXJGU खࡳͷϞσϧΫϥε import UIKit struct Hand { private var deck = Deck() private var cards = [Card]() var numberOfCards: Int { return cards.count } func cardAtPosition(index: Int) -> Card { return cards[index] }

Slide 37

Slide 37 text

͔͠͠ɺࠓճ͸EFDL͕ఆ਺ͳ ͷͰΧελϜΠχγϟϥΠβ ͰରԠ͢Δ

Slide 38

Slide 38 text

)BOETXJGU खࡳͷϞσϧΫϥε import UIKit struct Hand { private let deck = Deck() private var cards = [Card]() init() { } private init (deck: Deck, cards: [Card]) { self.cards = cards } var numberOfCards: Int { return cards.count }

Slide 39

Slide 39 text

)BOETXJGU खࡳͷߏ଄ମ௥ՃͰΠϯελϯεΛฦ͢ private func insertCard(card: Card, atIndex index: Int) -> Hand { var mutableCards = cards mutableCards.insert(card, atIndex: index) return Hand(deck: deck, cards: mutableCards) }

Slide 40

Slide 40 text

)BOE7$TXJGU खࡳͷ௥ՃΠϯελϯεΛड͚औΔ import UIKit class HandVC: UITableViewController { private var hand = Hand() override func viewDidLoad() { super.viewDidLoad() self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { if hand.numberOfCards < 5 { hand = hand.addNewCardAtIndex(0) insertTopRow() } }

Slide 41

Slide 41 text

)BOE7$TXJGU खࡳͷ࡟আͱҠಈ௥Ճͱಉ༷ʹ͍ͨ͠ override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { if editingStyle == .Delete { hand = hand.deleteCardAtIndex(indexPath.row) deleteRowAtIndexPath(indexPath) } } override func tableView(tableView: UITableView, moveRowAtIndexPath sourceIndexPath: NSIndexPath, toIndexPath destinationIndexPath: NSIndexPath) { hand = hand.moveCard(sourceIndexPath.row, toIndex: destinationIndexPath.row) }

Slide 42

Slide 42 text

)BOETXJGU खࡳͷߏ଄ମ࡟আͰΠϯελϯεΛฦ͢ mutating func deleteCardAtIndex(index: Int) { cards.removeAtIndex(index) } func deleteCardAtIndex(index: Int) -> Hand { var mutableCards = cards mutableCards.removeAtIndex(index) return Hand(deck: deck, cards: mutableCards) }

Slide 43

Slide 43 text

௥Ճ΍࡟আͱಉ༷ʹ Ҡಈ΋NVUBUJOHΛ֎͚͢Ͳ ͜͜ͰจԽΛௐ࿨ͤ͞Δ

Slide 44

Slide 44 text

'VODUJPOBM1SPHSBNNJOH ϑΝϯΫγϣφϧϓϩάϥϛϯά

Slide 45

Slide 45 text

)BOETXJGU खࡳͷߏ଄ମҠಈͰΠϯελϯεΛฦ͢ func moveCard(fromIndex: Int, toIndex: Int) -> Hand { return deleteCardAtIndex(fromIndex) .insertCard(cards[fromIndex], atIndex: toIndex) } mutating func moveCard(fromIndex: Int, toIndex: Int) { let cardToMove = cards[fromIndex] deleteCardAtIndex(fromIndex) insertCard(cardToMove, atIndex: toIndex) }

Slide 46

Slide 46 text

deleteCardAtIndex(fromIndex)ͷΠϯελϯε insertCard(cards[fromIndex], atIndex: toIndex)ͷΠϯελϯε cards[fromIndex] = ߏ଄ମͷϓϩύςΟ

Slide 47

Slide 47 text

– Daniel H Steinberg l͜ΕͰεϚΠϧʹͳΒͳ͍ͳΒ +BWBͳΓ3VCZͳΓ΍Ε͹͍͍ͱࢥ͍·͢Αʁz

Slide 48

Slide 48 text

.PWFUPB$VTUPN$FMM

Slide 49

Slide 49 text

.PEJpFE.77. .PEFM7JFX7JFX.PEFM

Slide 50

Slide 50 text

$BSE$FMMTXJGU खࡳΛදࣔ͢ΔΧελϜηϧʢ7JFXʣ import UIKit class CardCell: UITableViewCell { @IBOutlet private weak var suitLabel: UILabel! @IBOutlet private weak var rankLabel: UILabel! override func awakeFromNib() { super.awakeFromNib() } func fillWith(card: Card) { rankLabel.textColor = card.color rankLabel.text = card.rank.description suitLabel.text = card.suit.description } }

Slide 51

Slide 51 text

)BOE7$TXJGU खࡳΛදࣔ͢Δίϯτϩʔϥʔ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell else { fatalError("Could not create CardCell") } cell.fillWith(hand.cardAtPosition(indexPath.row)) return cell }

Slide 52

Slide 52 text

HVBSEMFUFMTFͷಛ௃ w 4XJGU͔Β௥Ճ͞Εͨɺ஋ͷόϦσʔγϣϯνΣοΫΛॻ͖΍͘͢͢Δߏจ w ΞϯϥοϓͱOJMνΣοΫΛಉ࣌ʹߦ͑ɺΞϯϥοϓ͞Εͨม਺͸HVBSEจΛൈ͚ͨޙͰ΋ ࢖༻Մ w HVBSEจͷFMTFઅͷதͰ͸ɺSFUVSO CSFBL DPOUJOVF UISPXͷͲΕ͔ΛߦΘͳ͍ͱί ϯύΠϧΤϥʔ http://qiita.com/mishimay/items/75fb0958f33079ff0e8a func show(message: String?) { guard let theMessage = message else { return } print(theMessage) // Ξϯϥοϓͨ͠ม਺Λ࢖͏͜ͱ͕Ͱ͖Δ }

Slide 53

Slide 53 text

)BOE7$TXJGU खࡳΛදࣔ͢Δίϯτϩʔϥʔ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell else { fatalError("Could not create CardCell") } cell.fillWith(hand.cardAtPosition(indexPath.row)) return cell }

Slide 54

Slide 54 text

TVCTDSJQUΛ࢖͏ w ഑ྻ΍ࣙॻͷཁૉʹ௚઀ΞΫηε͢ΔॲཧΛɺΫϥε΍ߏ଄ମʹࣗ෼ͰఆٛͰ͖Δ w ࠓճ͸ɺߏ଄ମʹͨ͠)BOEͷ$BSETͷཁૉʹΞΫηε͢ΔॲཧΛTVCTDSJQUͰه ड़͢Δ struct Hand { private let deck = Deck() private var cards = [Card]() var numberOfCards: Int { return cards.count } subscript(index: Int) -> Card { return cards[index] }

Slide 55

Slide 55 text

)BOE7$TXJGU खࡳΛදࣔ͢Δίϯτϩʔϥʔ override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell else { fatalError("Could not create CardCell") } cell.fillWith(hand.cardAtPosition(indexPath.row)) return cell } cell.fillWith(hand[indexPath.row]) return cell }

Slide 56

Slide 56 text

)BOE )BOE7$ .PEFM7JFX7JFX.PEFM $BSE$FMM

Slide 57

Slide 57 text

4FDPOE%FNP

Slide 58

Slide 58 text

)BOE )BOE7$ .PEFM7JFX7JFX.PEFM $BSE$FMM

Slide 59

Slide 59 text

– Daniel H Steinberg lօϞσϧΛ෼͔͍ͬͯΔঢ়ଶͰΑ͘ͳ͘ݟ͑Δʁ ͜Ε͔Βྑ͘ͳΔɻz

Slide 60

Slide 60 text

5BLFUIF%BUB4PVSDFPVUPG UIF5BCMF7JFX$POUSPMMFS

Slide 61

Slide 61 text

)BOE )BOE7$ %BUB4PVSDFΛ7JFX$POUSPMMFS ͔ΒऔΓग़͢ %BUB4PVSDF

Slide 62

Slide 62 text

%BUB4PVSDFTXJGU 7$͔ΒϞσϧґଘͷॲཧΛग़͢ import UIKit class DataSource: NSObject, UITableViewDataSource { private var hand = Hand() func addItemTo(tableView: UITableView) {} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {} func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {} func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {} func insertTopRowIn(tableView: UITableView) {} func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) {} }

Slide 63

Slide 63 text

)BOE7$TXJGU 7$ͰEBUB4PVSDFΛࢦఆ͢Δ import UIKit class HandVC: UITableViewController { private var dataSource = DataSource() override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = dataSource self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { dataSource.addItemTo(tableView) } }

Slide 64

Slide 64 text

)BOE )BOE7$ %BUB4PVSDF

Slide 65

Slide 65 text

%BUB4PVSDFTXJGU 7$͔ΒϞσϧґଘͷॲཧΛग़͢ import UIKit class DataSource: NSObject, UITableViewDataSource { private var hand = Hand() func addItemTo(tableView: UITableView) {} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {} func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {} func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {} func insertTopRowIn(tableView: UITableView) {} func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) {} }

Slide 66

Slide 66 text

– Daniel H Steinberg l͜ΕΒ͸ͪΐͬͱҧ͏ɻ Ҿ্͖͛Δɻ 1SPUPDPMΛ࢖͍·͢ɻz

Slide 67

Slide 67 text

1SPUPDPM0SJFOUFE 1SPHSBNNJOH ϓϩτίϧࢦ޲ϓϩάϥϛϯά

Slide 68

Slide 68 text

)BOE )BOE7$ %BUB4PVSDF 4PVSDF5ZQF

Slide 69

Slide 69 text

4PVSDF5ZQFTXJGU ϓϩτίϧͰҾ্͖͛Δ import UIKit protocol SourceType: UITableViewDataSource { func insertTopRowIn(tableView: UITableView) func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) } extension SourceType { func insertTopRowIn(tableView: UITableView) { tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } }

Slide 70

Slide 70 text

%BUB4PVSDFTXJGU %BUB4PVSDFΛ4PVSDF5ZQFʹ४ڌ import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType { private var hand = Hand() func addItemTo(tableView: UITableView) {} func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {} func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {} func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {} func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {} }

Slide 71

Slide 71 text

QSPUPDPMFYUFOTJPOͱ͸ʁ w QSPUPDPMʢΠϯλϑΣʔεͷఆٛʣΛ֦ு͠ɺϝιουͷ࣮૷Λ௥Ճ͢Δػೳ w ศརͳQSPUPDPMFYUFOTJPO͕ͩɺ֨ೲܕϓϩύςΟΛ࣋ͭ͜ͱ͕Ͱ͖ͳ͍ protocol MyProtocol { func hoge() } extension MyProtocol { func hoge() { print("hoge") } } class MyClass: MyProtocol { } let myInstance = MyClass() myInstance.hoge() // hoge ͱग़ྗ͞ΕΔ

Slide 72

Slide 72 text

)BOE )BOE7$ %BUB4PVSDF 4PVSDF5ZQF %BUB5ZQF

Slide 73

Slide 73 text

)BOETXJGU %BUB5ZQF΁Ҿ্͖͛Δલʹ import UIKit struct Hand { private let deck = Deck() private var cards = [Card]() init() {} private init (deck: Deck, cards: [Card]) {} subscript(index: Int) -> Card {} var numberOfCards: Int {} func addNewCardAtIndex(index: Int) -> Hand {} private func insertCard(card: Card, atIndex index: Int) -> Hand {} func deleteCardAtIndex(index: Int) -> Hand {} func moveCard(fromIndex: Int, toIndex: Int) -> Hand {} }

Slide 74

Slide 74 text

)BOETXJGU %BUB5ZQF΁Ҿ্͖͛Δલʹ import UIKit struct Hand { private let deck = Deck() private var cards = [Card]() init() {} private init (deck: Deck, cards: [Card]) {} subscript(index: Int) -> Card {} var numberOfItems: Int {} func addNewItemAtIndex(index: Int) -> Hand {} private func insertItem(card: Card, atIndex index: Int) -> Hand {} func deleteItemAtIndex(index: Int) -> Hand {} func moveItem(fromIndex: Int, toIndex: Int) -> Hand {} }

Slide 75

Slide 75 text

%BUB5ZQFTXJGU %BUB5ZQF΁Ҿ্͖͛Δ import UIKit protocol DataType { var numberOfItems: Int { get } func addNewItemAtIndex(index: Int) -> Self func deleteItemAtIndex(index: Int) -> Self func moveItem(fromIndex: Int, toIndex: Int) -> Self }

Slide 76

Slide 76 text

%BUB5ZQFTXJGU %BUB5ZQFʹ४ڌͨ͠΋ͷΛฦ͢ import UIKit protocol DataType { var numberOfItems: Int { get } func addNewItemAtIndex(index: Int) -> Self func deleteItemAtIndex(index: Int) -> Self func moveItem(fromIndex: Int, toIndex: Int) -> Self }

Slide 77

Slide 77 text

– Daniel H Steinberg l)BOEΛฦ͍ͯͨ͠ͷΛ4FMGͰฦ͢Α͏ʹ͢Δɻ ʢ4FMG͸ʣ%BUB5ZQFʹ४ڌ͍ͯ͠Δऀࣗ਎ɻz

Slide 78

Slide 78 text

)BOETXJGU )BOE͸%BUB5ZQFʹ४ڌ import UIKit struct Hand : DataType { private let deck = Deck() private var cards = [Card]() init() {} private init (deck: Deck, cards: [Card]) {} subscript(index: Int) -> Card {} var numberOfItems: Int {} func addNewItemAtIndex(index: Int) -> Hand {} private func insertItem(card: Card, atIndex index: Int) -> Hand {} func deleteItemAtIndex(index: Int) -> Hand {} func moveItem(fromIndex: Int, toIndex: Int) -> Hand {} }

Slide 79

Slide 79 text

)BOE )BOE7$ %BUB4PVSDF 4PVSDF5ZQF %BUB5ZQF

Slide 80

Slide 80 text

4PVSDF5ZQFTXJGU %BUB5ZQFΛϓϩύςΟʹએݴ import UIKit protocol SourceType: UITableViewDataSource { var dataObject: DataType { get set } func insertTopRowIn(tableView: UITableView) func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) } extension SourceType { func insertTopRowIn(tableView: UITableView) { tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } }

Slide 81

Slide 81 text

%BUB4PVSDFTXJGU )BOEΛ%BUB5ZQFͰड͚Δ import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType { var dataObject: DataType = Hand() func addItemTo(tableView: UITableView) { if dataObject.numberOfItems < 5 { dataObject = dataObject.addNewItemAtIndex(0) insertTopRowIn(tableView) } } }

Slide 82

Slide 82 text

%BUB4PVSDFTXJGU )BOE΋HVBSEͰ߹Θͤͯ൑ఆ͢Δ func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell, hand = dataObject as? Hand else { fatalError("Could not create CardCell or Hand instance") } cell.fillWith(hand[indexPath.row]) return cell }

Slide 83

Slide 83 text

5IJSE%FNP

Slide 84

Slide 84 text

– Daniel H Steinberg l͍͖ͭͯͯ·͢ʁ 4UPQͬͯݴͬͯ͘Εͯ΋͍͍Ͱ͢Αʁz

Slide 85

Slide 85 text

– Daniel H Steinberg lଞʹσʔλιʔε͔ΒϓϧΞ΢τग़དྷͳ͍͔ʁz

Slide 86

Slide 86 text

%BUB4PVSDFTXJGU import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType { var dataObject: DataType = Hand() func addItemTo(tableView: UITableView) { if dataObject.numberOfItems < 5 { dataObject = dataObject.addNewItemAtIndex(0) insertTopRowIn(tableView) } }

Slide 87

Slide 87 text

– Daniel H Steinberg l͜Ε͸ʢ͜͜ʹʣ͋Δ΂͖Ͱ͠ΐ͏͔ʁz

Slide 88

Slide 88 text

4PVSDF5ZQFTXJGU 4PVSDF5ZQFʹ൑ఆ஋Λ࣋ͨͤΔ import UIKit protocol SourceType: UITableViewDataSource { var dataObject: DataType { get set } var conditionForAdding: Bool { get } func insertTopRowIn(tableView: UITableView) func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) } extension SourceType { func insertTopRowIn(tableView: UITableView) { tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } }

Slide 89

Slide 89 text

4PVSDF5ZQFTXJGU 4PVSDF5ZQFʹ൑ఆ஋Λ࣋ͨͤΔ import UIKit protocol SourceType: UITableViewDataSource { var dataObject: DataType { get set } var conditionForAdding: Bool { get } func insertTopRowIn(tableView: UITableView) func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) } extension SourceType { func addItemTo(tableView: UITableView) { if conditionForAdding { dataObject = dataObject.addNewItemAtIndex(0) insertTopRowIn(tableView) } } func insertTopRowIn(tableView: UITableView) { tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 0, inSection: 0)], withRowAnimation: .Fade) } func deleteRowAtIndexPath(indexPath: NSIndexPath, from tableView: UITableView) { tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade) } }

Slide 90

Slide 90 text

%BUB4PVSDFTXJGU ਺ͷ൑ఆͷΈߦ͏ import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType { var dataObject: DataType = Hand() var conditionForAdding :Bool { return dataObject.numberOfItems < 5 } import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType { var dataObject: DataType = Hand() func addItemTo(tableView: UITableView) { if dataObject.numberOfItems < 5 { dataObject = dataObject.addNewItemAtIndex(0) insertTopRowIn(tableView) } }

Slide 91

Slide 91 text

)BOE )BOE7$ )BOE%BUB4PVSDF 4PVSDF5ZQF %BUB5ZQF %BUB4PVSDF

Slide 92

Slide 92 text

/PXQVTI)BOESFGFSFODF EPXOJOUPUIFTVCDMBTT

Slide 93

Slide 93 text

)BOE%BUB4PVSDFTXJGU %BUB4PVSDFͷαϒΫϥε࡞੒ import UIKit class HandDataSource: DataSource { init() { super.init(dataObject: Hand()) } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell, hand = dataObject as? Hand else { return UITableViewCell() } cell.fillWith(hand[indexPath.row]) return cell } override var conditionForAdding: Bool { return dataObject.numberOfItems < 5 } }

Slide 94

Slide 94 text

)BOE%BUB4PVSDFTXJGU %BUB4PVSDFͷαϒΫϥε࡞੒ import UIKit class HandDataSource: DataSource { init() { super.init(dataObject: Hand()) } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { guard let cell = tableView .dequeueReusableCellWithIdentifier("cardCell", forIndexPath: indexPath) as? CardCell, hand = dataObject as? Hand else { return UITableViewCell() } cell.fillWith(hand[indexPath.row]) return cell } override var conditionForAdding: Bool { return dataObject.numberOfItems < 5 } }

Slide 95

Slide 95 text

%BUB4PVSDFTXJGU %BUB4PVSDF͔ΒϞσϧґଘΛͳ͘͢ import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType { var dataObject: DataType var conditionForAdding :Bool { return false } import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType { var dataObject: DataType = Hand() var conditionForAdding :Bool { return dataObject.numberOfItems < 5 }

Slide 96

Slide 96 text

%BUB4PVSDFTXJGU %BUB4PVSDFͷΠχγϟϥΠβ࡞੒ import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType { var dataObject: DataType var conditionForAdding :Bool { return false } init(dataObject: A) { self.dataObject = dataObject }

Slide 97

Slide 97 text

)BOE7$TXJGU 7$Ͱ)BOE%BUB4PVSDFΛࢦఆ import UIKit class HandVC: UITableViewController { private var dataSource = HandDataSource() override func viewDidLoad() { super.viewDidLoad() tableView.dataSource = dataSource self.navigationItem.leftBarButtonItem = editButtonItem() } @IBAction private func addNewCard(sender: UIBarButtonItem) { dataSource.addItemTo(tableView) } }

Slide 98

Slide 98 text

'PSUI%FNP

Slide 99

Slide 99 text

– Daniel H Steinberg lͯ͞ɺԿΛ͠·͔ͨ͠ʁ ͍ΖΜͳ؆ૉԽ͠·ͨ͠ɻ มߋ͠ͳ͍ίʔυΛ֎ʹ௥͍ग़͠·ͨ͠ɻz

Slide 100

Slide 100 text

)BOE )BOE7$ )BOE%BUB4PVSDF 4PVSDF5ZQF %BUB5ZQF %BUB4PVSDF

Slide 101

Slide 101 text

– Daniel H Steinberg lมߋ͢Δ΋ͷΛԼʹ࣋ͬͯདྷ·ͨ͠ɻz

Slide 102

Slide 102 text

)BOE )BOE7$ )BOE%BUB4PVSDF 4PVSDF5ZQF %BUB5ZQF %BUB4PVSDF

Slide 103

Slide 103 text

#MFOEJOH$VMUVSF $POUSPMMFS .PEFM 7JFX 001 101 '1

Slide 104

Slide 104 text

5SZl#MFOEJOH$VMUVSFzΛ΍ͬ ͯΈͨײ૝ w ൃද࣌ؒʹΑΓɺ΍Ήͳ͘লུ͞Εͯ͠·ͬͨ෦෼ ͕͋Δͷʹ΍ͬͯΈͯؾͮ͘ɻ w ͳͷͰɺࣗ෼ͰखΛಈ͔͠ͳ͕Β࣮ࡍʹ΍ͬͯΈΔ ͜ͱΛڧ͓͘קΊ͢Δ w αϯϓϧͰ͸ͳ͘ɺ΋͏গ͠ػೳͷଟ͍ϓϩδΣΫ τʹಋೖ͢ΔͱɺͲͷΑ͏ʹιʔε͕มԽ͢Δͷ͔ɺ ·ͩมԽͤ͟ΔΛಘͳ͍ͷ͔ɺUSZͯ͠Έ͍ͨɻ

Slide 105

Slide 105 text

ͦΕͧΕͷ%FNP࣌఺ͷιʔε͸ɺ ԼهͷϦϙδτϦʹϒϥϯνΛ੾ͬ ͯอଘͨ͠ IUUQTHJUIVCDPNNBTBTIJTVUPV #MFOEJOH$VMUVSFT&YBNQMF

Slide 106

Slide 106 text

ࢀߟࢿྉ http://niwatako.hatenablog.jp/entry/2016/03/02/160011 ɾʮUSZ4XJGUจԽΛௐ࿨ͤ͞ΔUSZTXJGUDPOG%BZʯ ɾʮUSZ4XJGU࠷଎Ϩϙʔτ೔໨ޕޙUSZTXJGUDPOGʯ http://dev.classmethod.jp/smartphone/tryswift-1st-day-pm/ ɾʮUSZ4XJGU೔໨#MFOEJOH$VMUVSFTzͷ಺༰Λ αϯϓϧΞϓϦʹىͯ͜͠Έͨʯ http://qiita.com/su_k/items/42c29a0e136faa3f9e08 ɾʮ#MFOEJOH$VMUVSFʯUSZ4XJGU Daniel Steinberg @dimsumthinking

Slide 107

Slide 107 text

ࢀߟࢿྉ http://qiita.com/mishimay/items/75fb0958f33079ff0e8a ɾʮ4XJGUͰ௥Ճ͞ΕͨHVBSEͷ͍͍ͱ͜Ζʯ ɾʮ4XJGUͷ4VCTDSJQUʹ͍ͭͯʯ http://qiita.com/happy_ryo/items/72b68859ed8ace9f5fb4 ɾʮ4XJGUͷQSPUPDPMFYUFOTJPOͰNJYJOతͳ΋ͷΛ ࣮ݱ͢Δʯ http://qiita.com/sagaraya/items/5e73501ff7a3f0c502c0 ɾʮ4XJGUͷΫϥεͱߏ଄ମͷ࢖͍෼͚ʹ͍ͭͯͷϝϞʯ http://qiita.com/mini_house/items/22b7b88794b9f8a32381