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

「文化を調和させる」 をやってみる Try “Blending Culture”

「文化を調和させる」 をやってみる Try “Blending Culture”

■ Special Thanks To:
- Daniel H Steinberg
Twitter(https://twitter.com/dimsumthinking
Web Site(http://dimsumthinking.com/

■ Sample repo
- https://github.com/masashi-sutou/BlendingCulturesExample

Masashi-Sutou

March 11, 2016
Tweet

More Decks by Masashi-Sutou

Other Decks in Programming

Transcript

  1. ʮจԽΛௐ࿨ͤ͞Δʯͱ͸ʁ w USZ 4XJGUͷ̍೔໨ʹߦΘΕͨ %BOJFM)4UFJOCFSHࢯͷൃද ʮ#MFOEJOH$VMUVSFʯͷ͜ͱ w 4XJGUΒ͍͠.7$Ϟσϧͷ 5BCMF7JFX$POUSPMMFSͷ࡞Γ ํΛ঺հ͍ͯͨ͠

    w USZ4XJGU͔ͩΒɺ5SZ l#MFOEJOH$VMUVSFzͯ͜ͱ Ͱɺ౰೔Λࢥ͍ग़ͯࣗ͠෼Ͱ ΍ͬͯΈΔ Ҿ༻ݩɿIUUQEFWDMBTTNFUIPEKQTNBSUQIPOFUSZTXJGUTUEBZQN 
  2. ௐ࿨͍ͤͨ͞จԽͨͪ w 0010CKFDU0SJFOUFE1SPHSBNNJOH ΦϒδΣΫτ ࢦ޲ϓϩάϥϛϯά  w 1011SPUPDPM0SJFOUFE1SPHSBNNJOH ϓϩτίϧࢦ ޲ϓϩάϥϛϯά

     w '1'VODUJPOBM1SPHSBNNJOH ϑΝϯΫγϣφϧϓϩ άϥϛϯά  w .7$.PEFM7JFX$POUSPMMFS ϞσϧɾϏϡʔɾίϯ τϩʔϥʔ 
  3. )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) } } 
  4. )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) }
  5. )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 }
  6. )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) } }
  7. )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) }
  8. )BOE7$TXJGU खࡳͷҠಈ  override func tableView(tableView: UITableView moveRowAtIndexPath sourceIndexPath: NSIndexPath,

    toIndexPath destinationIndexPath: NSIndexPath) { hand.moveCard(sourceIndexPath.row, toIndex: destinationIndexPath.row) }
  9. )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] }
  10. )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) }
  11. )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) }
  12. )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] }
  13. )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) }
  14. )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) } } 
  15. )BOETXJGU खࡳͷߏ଄ମ௥ՃͷNVUBUJOHΛ֎͢  mutating func addNewCardAtIndex(index: Int) { insertCard(deck.nextCard(), atIndex:

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

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

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

    -> Hand { var mutableCards = cards mutableCards.insert(card, atIndex: index) return Hand(deck: deck, cards: mutableCards) }
  21. )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() } }
  22. )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) }
  23. )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) }
  24. )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) }
  25. $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 } }
  26. )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 }
  27. 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) // Ξϯϥοϓͨ͠ม਺Λ࢖͏͜ͱ͕Ͱ͖Δ }
  28. )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 }
  29. 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] }
  30. )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 }
  31. %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) {} }
  32. )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) } }
  33. %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) {} }
  34. 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) } }
  35. %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) {} }
  36. QSPUPDPMFYUFOTJPOͱ͸ʁ w QSPUPDPMʢΠϯλϑΣʔεͷఆٛʣΛ֦ு͠ɺϝιουͷ࣮૷Λ௥Ճ͢Δػೳ w ศརͳQSPUPDPMFYUFOTJPO͕ͩɺ֨ೲܕϓϩύςΟΛ࣋ͭ͜ͱ͕Ͱ͖ͳ͍  protocol MyProtocol { func

    hoge() } extension MyProtocol { func hoge() { print("hoge") } } class MyClass: MyProtocol { } let myInstance = MyClass() myInstance.hoge() // hoge ͱग़ྗ͞ΕΔ
  37. )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 {} }
  38. )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 {} }
  39. %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 }
  40. %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 }
  41. )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 {} }
  42. 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) } }
  43. %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) } } }
  44. %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 }
  45. %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) } }
  46. 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) } }
  47. 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) } }
  48. %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) } }
  49. )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 } }
  50. )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 } }
  51. %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 }
  52. %BUB4PVSDFTXJGU %BUB4PVSDFͷΠχγϟϥΠβ࡞੒  import UIKit class DataSource: NSObject, UITableViewDataSource, SourceType

    { var dataObject: DataType var conditionForAdding :Bool { return false } init<A: DataType>(dataObject: A) { self.dataObject = dataObject }
  53. )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) } }