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

Swift愛好会初心者ハンズオン(vol.31)

 Swift愛好会初心者ハンズオン(vol.31)

2cec9566cc4f761ac87433573c31f1a0?s=128

Satoru Ohguchi

May 20, 2018
Tweet

More Decks by Satoru Ohguchi

Other Decks in Technology

Transcript

  1. 4XJGUѪ޷ձ ॳ৺ऀϋϯζΦϯ  SwiftѪ޷ձ #31

  2. ΨΠμϯε • SwiftѪ޷ձͷ৽͍͠औΓ૊Έͱͯ͠ɺॳ৺ऀϋϯ ζΦϯΛ΍ͬͯΈΑ͏ͱ͍͏͜ͱʹͳΓ·ͨ͠ɻ • ༗ࢤʹΑΔϋϯζΦϯͱͳΓ·͢ͷͰɺࢸΒͳ͍ ఺ͳͲ͋Δ͔΋͠Ε·ͤΜɻ͝ڠྗɺ͝ཧղ͍ͨͩ ͖·͢Α͏͓ئ͍͍ͨ͠·͢ɻ

  3. ࣗݾ঺հ • ࢘ձਐߦ໾ͷ঺հ • Reiko Goto(@burakon) • Satoru Ohguchi(@o_chicchi) •

    ϝϯλʔਞͷ঺հ • Ұݴͣͭࣗݾ঺հԼ͍͞ɻ
  4. ͸͡Ίʹ • ຊ೔ͷਐΊํ • αϯϓϧϓϩάϥϜΛ׬੒͢Δ͜ͱΛΰʔϧͱ͠·͢ɻ • ࠷ॳʹ֓ཁͳͲΛઆ໌͠·͢ɻ • ͻͱ·ͱ·Γຖʹઆ໌ɺ࣮ࡍ΍ͬͯ௖͘ͷ܁Γฦ͠Ͱ͢ɻ •

    ਵ࣌ɺ࣭໰͍ͯͩ͘͠͞ɻ • ૢ࡞ͷ෼͔Βͳ͍఺ͳͲ͸ɺϝϯλʔʹϔϧϓΛٻΊͯ Լ͍͞ɻ • άϧʔϓͰॿ͚߹͍ͳ͕ΒਐΊ͍͚ͯͨͩΔ͜ͱ΋ظ଴͠ ͍ͯ·͢ɻ •
  5. ࣗݾ঺հ • ໊͓લɺχοΫωʔϜͰ΋Α͍Ͱ͢ɻ • ֶੜ? ࣾձਓ? • ;ͩΜ΍͍ͬͯΔ͜ͱ(࢓ࣄ΍झຯͷ͜ͱͱ͔ɺઐ ߈ͷ͜ͱͱ͔) •

    SwiftҎ֎ͷݴޠͷܦݧͱɺSwiftͷܦݧ
  6. ࢀՃऀΞϯέʔτ(1) •ֶੜͰ͋Δ •ए͍ࣾձਓͰ͋Δ •΋͏ए͘ͳ͍ࣾձਓͰ͋Δ •·ͩ·ͩݩؾͳ೥دΓͰ͋Δ

  7. ࢀՃऀΞϯέʔτ(2) • ࢲ͸ɺ·ͬͨ͘ͷॳ৺ऀͰɺMacΛ৮Δͷ΋ɺϓ ϩάϥϜ͢Δͷ΋ॳΊͯͩɻ(͓͓ΉͶ1೥ະຬ) • ଞͷݴޠͰͷϓϩάϥϛϯάܦݧ͸͋Δ(ઙͯ͘΋ Մ)ɺiOSͷϓϩάϥϜΛॻ͍ͯΈ͍͔ͨΒɺࠓ೔͸ དྷͯΈͨɻ • ΞϓϦΛ࡞ͬͯΈͨͯ͘ɺॻ੶ͳͲͰֶश͍ͯ͠

    Δ͕ɺͳ͔ͳ͔शಘʹࢸΒͣɺۤ࿑͍ͯ͠Δɻ
  8. ࿩͞ͳ͍͜ͱ • Storyboard ΋গ͚͔ͩ͠͠΍Γ·ͤΜɻ • ڽͬͨϨΠΞ΢τ΍ɺਂ͍ϓϩάϥϛϯάʹ͍ͭ ͯ͸ѻ͍·ͤΜɻ • ઃܭख๏ͱ͍ͬͨ͜ͱʹ͸৮Ε·ͤΜɻςετप ลͷίʔυ΋৮Ε·ͤΜɻ

    • ΞϓϦͷϦϦʔεʹ޲͚ͨొ࿥ํ๏ɺసૹํ๏ʹ ͍ͭͯ͸৮Ε·ͤΜɻ
  9. ϓϩάϥϛϯάֶशʹ͍ͭͯ

  10. ϓϩάϥϜΛश͏ಈػ • Կ͔࡞ͬͯΈ͍ͨαʔϏεɺΞϓϦ͕͋ͬͯɺͦ ͷͨΊʹֶश͢Δɻ • ब৬ɺస৬׆ಈʹ༗ར͔ͩΒɺֶशͯ͠༗རʹਐ Ί͍ͨɻ • Ͱ΋ɺαʔϏε΍ΞϓϦΛ׬੒ͤ͞Δ͜ͱ͕ॏཁ ͰɺʮJavaΛश͍ͬͯ·͢ʯͱ͍͏ΑΓɺ੒Ռ෺͕

    ͋Δ΄͏͕Α͍ɻ
  11. ϓϩάϥϛϯάݴޠʹ͍ͭͯ • Ͳͷݴޠ͔Βೖͬͯ΋ɺͲΕͰ΋͍͍ͱࢥ͏ɻͰ ΋ɺࠓ೔͸ɺSwiftɻSwift ͸ਪ঑Ͱ͖ΔɺΑ͘Ͱ͖ ͨݴޠͩͱࢥ͏ɻ • ಘҙͳݴޠΛɺ1ͭ࣋ͭ͜ͱ͕ॏཁɻಘҙͳݴޠΛ ෳ਺ʹͰ͖ͨΒɺͰ͖Δ͜ͱ͕޿͕Δɻ •

    1ͭͷݴޠΛशಘͰ͖Ε͹ɺଞͷݴޠʹ޿͍͛ͯ͘ ͜ͱ͸ɺͦΜͳʹ೉͘͠ͳ͍ɻ
  12. ϓϩάϥϛϯάֶशͷίπ • ίϐʔ&ϖʔετ͚ͩͰ͸ɺͳ͔ͳֶ͔श͸೉͍͠ɻ • ࣮ࡍʹɺλΠϓͯ͠ɺಈ͖Λ֬ೝ͠ͳ͕Β΍Δͷ͕ Α͍ɻ • ͳͥɺ͜͏ಈ͘ͷ͔ɺϩδοΫ͕Ͳ͏ͳ͍ͬͯΔͷ ͔ɺཧղͯ͠ਐΊΔ͜ͱ͕ॏཁɻࢴʹॻ͍ͯΈͨΓɺ ੔ཧͯ͠ΈͨΓɻ

    • ޠֶͱҰॹͰগͣͭ͠Ͱ΋ຖ೔ɺຊಡΜͩΓɺ৮Ε ͨΓ͢Δ͜ͱ͕ਐาʹܨ͕Δɻ
  13. ݸਓతʹ͸… • ϓϩάϥϛϯά͸ɺࠓޙɺখֶੜͷΧϦΩϡϥϜʹ ೖ͖ͬͯͯ޿͕͍ͬͯ͘͜ͱ͕༧૝͞ΕΔɻجຊ஌ ࣝͷྖҬʹۙ͘ͳ͍ͬͯ͘ͱࢥ͏ɻ • ΞϓϦΛ࡞ͬͨΓɺશੈքʹϦϦʔεͰ͖ͨΓ͢Δ ࢓૊Έ͸໘ന͍ɻपล஌ࣝ͸͍Ζ͍ΖඞཁͰ͕͢ɺ νϟϨϯδ͢ΔՁ஋͸͋Δͱࢥ͍·͢ɻ •

    ੲ͔Βɺίϯϐϡʔλʔ͕޷͖ͳਓͱͯ͠͸ɺίϯ ϐϡʔλʔ΍ϓϩάϥϛϯά͕ɺ޷͖ʹͳͬͯ͘Ε Δਓ͕ҰਓͰ΋૿͑ͨΒخ͍͠ɻ
  14. ϋϯζΦϯͷਐΊํ

  15. ϋϯζΦϯࢿྉ w $PNNQBTT্ͷϦϯΫ͔ΒɺHJUIVC্ͷखॱΛࢀরԼ ͍͞ɻ w εϥΠυ্Ͱ͸ɺओͳखॱͷΈࣔ͠·͢ɻ w ਐΊΒΕΔํ͸ɺखॱΛݟͳ͕Βઌ΁ਐΊͯԼ͍͞ɻ

  16. Xcodeͷॳظઃఆ

  17. XcodeΛىಈ͢Δ • ΞϓϦέʔγϣϯ → Xcode ͱબΜͰىಈ

  18. ઃఆը໘Λબ୒ w 9DPEFˠ1SFGFSFODFTͱબΜͰىಈ

  19. Xcodeͷॳظઃఆ(1) • Accounts Λબ୒͢Δɻ

  20. Xcodeͷॳظઃఆ(1) • ࠨԼʹ͋ΔʮʴʯΛબ୒͢Δɻ

  21. Xcodeͷॳظઃఆ(1) • Apple ID Λબ୒͠ɺContinue Λબ୒͢Δɻ

  22. Xcodeͷॳظઃఆ(1) • AppleIDͷΞΧ΢ϯτΛઃఆ͠ɺSign In Λબ୒͢Δɻ

  23. Xcodeͷॳظઃఆ(2) •ߦදࣔΛग़͢ Text Editing ͷதͷ Line Number Λબ୒͢Δɻ

  24. Xcodeͷॳظઃఆ(3) w %PVCMF$MJDL/BWJHBUJPOΛઃఆ͢Δɻ

  25. Hello World Λग़ͯ͠ΈΔ

  26. ϓϩδΣΫτΛ࡞੒͢Δ • File → New → Project

  27. ΞϓϦͷλΠϓΛબ୒͢Δ • Single View App → Next Λબ୒

  28. ϓϩδΣΫτͷඞཁࣄ߲Λઃఆ •ҎԼͷΑ͏ʹهೖͯ͠ɺNext Λબ୒ɻ

  29. อ؅ઌΛબ୒ • อ؅৔ॴΛઃఆͯ͠ɺʮCreateʯΛબ୒͢Δɻ

  30. Storyboard Λબ୒ •Xcode͕։͘ͷͰɺStoryboard Λબ୒

  31. SafeAreaΛબ୒ •Safe Area Λબ୒͢Δ

  32. Hello World ͷϥϕϧΛ഑ஔ͢Δ • Xcode ͷӈԼʹ͋Δը໘ͷը໘Ͱɺʮshow the object libraryʯΛબ୒ •

    Filter ʹ Label ͱೖྗ • Label ͕දࣔ͞ΕΔ͜ͱΛ֬ೝ
  33. Hello World ͷϥϕϧΛ഑ஔ͢Δ

  34. Hello World ʹจࣈྻΛॻ͖׵͑Δ

  35. ੍໿Λઃఆ͢Δ

  36. ԼهͷΑ͏ʹͳ͍ͬͯΔ͜ͱΛ֬ೝ

  37. Ϗϧυ࣮ͯ͠ߦͯ͠ΈΔ • iPhoneX ͷγϛϡϨʔλʔΛબΜͰʮ⾣ʯΛબ୒͢Δɻ

  38. ΫΠζΞϓϦ

  39. ૉࡐͷμ΢ϯϩʔυ • ΫΠζͷ໰୊ͷcsvٴͼɺը૾Λ༻ҙͨ͠ͷͰɺμ ΢ϯϩʔυ͓ئ͍͠·͢ɻ

  40. શମͷྲྀΕͷઆ໌ • ࠓճͷΞϓϦͷશମతͳྲྀΕΛҎԼʹࣔ͠·͢ɻ ? ?

  41. 4UPSZ#PBSE

  42. Xcode Ͱ৽͍͠ϓϩδΣΫτΛ։͘

  43. ը໘ͷૉࡐΛొ࿥͢Δ w "TTFUTYDBTTFUTΛબ୒͢Δɻ "TTFUTYDBTTFUT

  44. ը໘ͷૉࡐΛొ࿥͢Δ •ը໘ͷૉࡐΛ഑ஔ͢Δɻ

  45. ViewControllerͷ഑ஔ • StoryBoard Λબ୒ͯ͠ɺදࣔ͠ɺViewController Λ഑ஔ͢Δɻ

  46. ֤ը໘༻ͷίʔυΛ४උ w 'JMFΛબ୒͠·͢ɻ

  47. ֤ը໘༻ͷίʔυΛ४උ w $PDPB5PVDIΛબ୒͢Δ

  48. w ΫϥεΛࢦఆ͠·͢ɻ2VJ[7JFX$POUSPMMFSͱࢦఆ͢Δɻ ֤ը໘༻ͷίʔυΛ४උ

  49. ֤ը໘༻ͷίʔυΛ४උ w ʮ$SFBUFʯΛબ୒͠·͢ɻ

  50. ֤ը໘༻ͷίʔυΛ४උ w ಉ༷ʹɺ3FTVMU7JFX$POUSPMMFSΛ࡞੒͠·͢ɻ

  51. $VTUPN$MBTTͷࢦఆ w ֤ετʔϦʔϘʔυʹࢦఆ͠·͢ɻ

  52. $VTUPN$MBTTͷࢦఆ w ಉ༷ʹɺ3FTVMU7JFX$POUSPMMFS΋ࢦఆ

  53. ෦඼഑ஔ

  54. શମ׬੒ਤ

  55. τοϓը໘ w *NBHF7JFXͱ#VUUPOΛ഑ஔ͠·͢ɻ

  56. ΫΠζදࣔը໘ w ҎԼ഑ஔ͠·͢ɻ w ϥϕϧ͇ w Ϙλϯ͇ w UBHΛઃఆ w

    *NBHF7JFXº
  57. ݁Ռදࣔը໘ w ҎԼΛ഑ஔ͠·͢ɻ w *NBHF7JFX w ϥϕϧ w Ϙλϯ

  58. ֤ը໘ͷඥ෇͚ w ελʔτϘλϯ͔Βɺ 2VJ[7JFX$POUSPMMFS ΁Ή͔ͬͯɺTFHVF Λઃఆ͢Δɻ

  59. ը໘ͱͷඥ෇͚ w 2VJ[7JFX$POUSPMMFS ͔Βɺ 3FTVMU7JFX$POUSPMMFS ʹ޲͔ͬͯɺTFHVF Λઃఆ͢Δɻ

  60. ը໘ͱͷඥ෇͚ w *EFOUJpFSΛఆٛ͢Δ

  61. 2VJ[7JFX$POUSPMMFSͷιʔεͱͷඥ෇͚

  62. 2VJ[7JFX$POUSPMMFSͷιʔεͱͷඥ෇͚

  63. 3FTVMU7JFX$POUSPMMFSͷιʔεͱͷඥ෇͚

  64. ϓϩάϥϛϯά

  65. ม਺Λఆٛ // ໰୊Λ֨ೲ var csvArray:[String] = [] // औΓग़ͨ͠໰୊Λ֨ೲ var

    questionArray:[String] = [] // ม਺ var questionCount = 0 // ग़୊਺ var correctCount = 0 // ਖ਼ղ // ఆ਺ let total = 3 // ग़୊਺ let waitQuestion = 0.8 // දࣔͷִؒ
  66. ໰୊ΛDTW͔Β഑ྻʹ // CSVϑΝΠϧͷಡΈࠐΈ func loadCSV(filename : String)->[String] { // ฦ٫஋ͷॳظԽ

    var csvStr:[String] = [] // CSVΛಡΈࠐΜͰArrayʹ if let csvPath = Bundle.main.path(forResource: filename, ofType: "csv") { do { var csvData = try String(contentsOfFile: csvPath, ɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹencoding: String.Encoding.utf8) csvData = csvData.replacingOccurrences(of: "\n", with: "") csvStr = csvData.components(separatedBy: .newlines) } catch let error as NSError { print(error.localizedDescription) } } return csvStr }
  67. ໰୊දࣔ // ໰୊දࣔ func nextProblem() { // questionArrayͷॳظԽ questionArray.removeAll(keepingCapacity: true)

    if questionCount < total { // questionArrayʹ৽ن໰୊Λ֨ೲ questionArray = csvArray[questionCount].components(separatedBy: ",") // දࣔ಺༰Ληοτ quizNumberLabel.text = "ୈ\(questionCount+1)໰" quizQuestion.text = questionArray[0] uiButton1.setTitle(questionArray[2], for: .normal) uiButton2.setTitle(questionArray[3], for: .normal) uiButton3.setTitle(questionArray[4], for: .normal) uiButton4.setTitle(questionArray[5], for: .normal) } else { // ໰୊Λશͯදࣔͨ͠ޙͷॲཧ(ޙͰهड़) } } }
  68. ճ౴Λબ୒ͨ͠Β൑ఆ // ਖ਼ޡ൑ఆ @IBAction func btnAction(_ sender: UIButton) { //

    ਖ਼ղͱϘλϯͷλά൪߸͕ಉ͡ͳΒਖ਼ղ if sender.tag == Int(questionArray[1]) { answerImage.image = UIImage(named: "correct.png") correctCount += 1 } else { answerImage.image = UIImage(named: "incorrect.png") } // ճ౴දࣔͷ༗ޮԽ(0.8ඵؒ) answerImage.isHidden = false DispatchQueue.main.asyncAfter(deadline: .now() + waitQuestion) { self.answerImage.isHidden = true } // ࣍ͷ໰୊จݺͼग़͠ questionCount += 1 nextProblem() }
  69. ը໘ભҠΛهड़ w 4FRVFΛ࢖ͬͯ஋Λ౉͢ͷͰ౉͢஋Λఆٛ w ը໘ભҠΛ͢Δ // Segue Ͱ஋Λ౉͢ override func

    prepare(for segue: UIStoryboardSegue, sender: Any?) { let sVC: ResultViewController = (segue.destination as? ResultViewController)! sVC.correct = correctCount } // Storyboard Segue ͷ Identifier ΛҾ਺ʹఆٛͯ͠ը໘ભҠ DispatchQueue.main.asyncAfter(deadline: .now() + waitQuestion) { self.performSegue(withIdentifier: "toResultViewController", sender: nil) }
  70. 2VJ[ͷWJFX%JE-PBEΛهड़ // ը໘දࣔ׬ྃޙʹಈ͘ϝιου override func viewDidLoad() { super.viewDidLoad() // ໰୊ͷಡΈࠐΈ

    csvArray = loadCSV(filename: "loveswift") // ը໘ͷॳظԽ answerImage.isHidden = true // ໰୊දࣔ nextProblem() }
  71. ը໘Λ໭ΔఆٛΛ͢Δ ViewControllerʹҎԼɺ௥ه͠·͢ɻ @IBAction func unWindSegue(_ sender: UIStoryboardSegue) { print("topwindow") }

  72. 4UPSZCPBSE্Ͱ໭ΔఆٛΛ͢Δ w VO8JOE4FRVFͱ݁ͼ͚ͭΔ

  73. Ϗϧυ • iPhoneX ͷγϛϡϨʔλʔΛબΜͰʮ⾣ʯΛબ୒ ͢Δɻ

  74. Τϥʔͷ֬ೝํ๏ w ը໘ͷԼ෦ʹҎԼͷ૭Λදࣔͤ͞Δ͜ͱͰɺΤϥʔΛ֬ೝ Ͱ͖·͢ɻ

  75. ׬੒Ͱ͖·͔ͨ͠

  76. ֶशΛଓ͚͍ͯ͘ʹ͸

  77. ֶशܧଓͷख๏ w ॻ੶ͰֶͿ w ωοτͰฉ͘ w ษڧձɺΧϯϑΝϨϯεʹࢀՃ͢Δ

  78. J1IPOFΞϓϦ௒։ൃೖ໳ w ࠓճͷϋϯζΦϯʹɺ͔ͳΓ ͍ۙ಺༰ͰͷΞϓϦ։ൃͷํ ๏Λઆ໌͍ͯ͠·͢ɻ w ࠓճͷϋϯζΦϯͷ෮श΍ɺ ೖ໳ऀ޲͚ͷॻ੶ͱͯ͠͸ɺ ࠷దͩͱࢥ͍·͢ɻ w

    ͜ͷॻ੶Λҙࣝͯ͠ࠓճͷϋ ϯζΦϯΛͨ͠༁Ͱ͸ͳ͍Ͱ ͕͢ɺهड़Ϩϕϧ͸ɺ΄΅ಉ ͡Ͱ͢ɻ
  79. J04ΞϓϦ։ൃूதߨ࠲ w ͍Ζ͍ΖͳΞϓϦͷ։ൃྫ͕ ܝࡌ͞Ε͍ͯ·͢ɻ೔Ͱ͸ऴ ΘΒͳ͍ϘϦϡʔϜͰ͢ɻ w ҰͭҰͭखॱʹԊͬͯ࡞ͬͯ ͍͘͜ͱͰ੡࡞ՄೳͰ͢ɻ w ೖ໳ऀ޲ֶ͚शॻ੶ͷ࡭໨ͱ

    ͓ͯ͠קΊͰ͢ɻ
  80. ຊؾͰ͸͡ΊΔJ1IPOFΞϓϦ࡞Γ w ௨শɺࠇଳຊͱݺ͹Ε͍ͯΔ ఆ൪ॻ੶Ͱ͢ɻ w جຊతͳςΫχοΫͱ͔Α͘ ॻ͔Ε͍ͯ·͢ɻ w ॳڃऀ͕தڃऀʹͳΔͨΊʹ ͸ɺඞಡͷ࡭ͱࢥ͍·͢ɻ

  81. 4XJGU࣮ફೖ໳ w ࠓճ͸ɺϓϩάϥϜίʔυͷ આ໌͸ল͍͍ͯ·͕͢ɺ4XJGU ͷॻ͖ํΛֶश͍ͨ͠ͷͰ͋ Ε͹ɺ࣮ફೖ໳͕ྑ͍ͱࢥ͍ ·͢ɻ w தڃऀʹͳΔͨΊʹ͸ಡΉ΂ ͖ॻ੶ͱࢥ͍·͢ɻ

    w ݸਓతʹ͸ಡΈ΍ͯ͘͢޷͖ ͳॻ੶Ͱ͢ɻ
  82. J041SPHSBNJOH w ॻళͷళ಄ʹ͸ฒΜͰ͍·ͤΜ ͕ɺQFBLT͔ΒΫϥ΢υϑΝϯ σΟϯάͰൢച͞Εͨॻ੶Ͱ͢ɻ w J04%FWFMPQFSͰ༗໊ͳํ͕ த৺ͱͳͬͯॻ͔Ε͍ͯΔͷͰɺ ͱͯ΋ࢀߟʹͳΔ࡭Ͱ͢ɻ w

    ͲͪΒ͔ͱ͍͏ͱɺதʙ্ڃऀ ޲͚ͱࢥ͍·͢ɻ w IUUQTQFBLTDDJ04
  83. UFSBUBJM w ࠔͬͨΒɺUFSBUBJMͰฉ͍ͯΈΔɻ

  84. 5IBOLZPV