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

Swift愛好会 #28

Swift愛好会 #28

Satoru Ohguchi

February 18, 2018
Tweet

More Decks by Satoru Ohguchi

Other Decks in Technology

Transcript

  1. ͸͡Ίʹ • ຊ೔ͷਐΊํ • αϯϓϧϓϩάϥϜΛ׬੒͢Δ͜ͱΛΰʔϧͱ͠·͢ɻ • ࠷ॳʹ֓ཁͳͲΛઆ໌͠·͢ɻ • ͻͱ·ͱ·Γຖʹઆ໌ɺ࣮ࡍ΍ͬͯ௖͘ͷ܁Γฦ͠Ͱ͢ɻ •

    ਵ࣌ɺ࣭໰͍ͯͩ͘͠͞ɻ • ૢ࡞ͷ෼͔Βͳ͍఺ͳͲ͸ɺϝϯλʔʹϔϧϓΛٻΊͯ Լ͍͞ɻ • άϧʔϓͰॿ͚߹͍ͳ͕ΒਐΊ͍͚ͯͨͩΔ͜ͱ΋ظ଴͠ ͍ͯ·͢ɻ
  2. ࿩͞ͳ͍͜ͱ • Swiftͷϓϩάϥϛϯά͸গ͚͔ͩ͠͠΍Γ·ͤΜɻ • Storyboard ΋গ͚͔ͩ͠͠΍Γ·ͤΜɻ • ڽͬͨϨΠΞ΢τ΍ɺਂ͍ϓϩάϥϛϯάʹ͍ͭͯ ͸ѻ͍·ͤΜɻ •

    ઃܭख๏ͱ͍ͬͨ͜ͱʹ͸৮Ε·ͤΜɻςετपล ͷίʔυ΋৮Ε·ͤΜɻ • ΞϓϦͷϦϦʔεʹ޲͚ͨొ࿥ํ๏ɺసૹํ๏ʹͭ ͍ͯ͸৮Ε·ͤΜɻ
  3. ·ͱΊ •࣌ؒͷؔ܎ͰɺऔΓ্͍͛ͯͳ͍͜ͱ͕ଟ਺͋Γ·͢ɻ • Switchจ • forจ • whileจ • ߏ଄ମ

    • είʔϓ • Ψʔυ • ϓϩτίϧ •…ͳͲͳͲ •Swiftͷॻ੶΍ɺAppleͷυΩϡϝϯτΛੋඇಡΜͰΈͯԼ͍͞ɻ
  4. ม਺Λఆٛ // ໰୊Λ֨ೲ var csvArray:[String] = [] // औΓग़ͨ͠໰୊Λ֨ೲ var

    questionArray:[String] = [] // ม਺ var questionCount = 0 // ग़୊਺ var correctCount = 0 // ਖ਼ղ // ఆ਺ let total = 3 // ग़୊਺ let waitQuestion = 0.8 // දࣔͷִؒ
  5. ໰୊Λ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 }
  6. ໰୊දࣔ // ໰୊දࣔ 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 { // ໰୊Λશͯදࣔͨ͠ޙͷॲཧ(ޙͰهड़) } } }
  7. ճ౴Λબ୒ͨ͠Β൑ఆ // ਖ਼ޡ൑ఆ @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() }
  8. ը໘ભҠΛهड़ 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) }
  9. 2VJ[ͷWJFX%JE-PBEΛهड़ // ը໘දࣔ׬ྃޙʹಈ͘ϝιου override func viewDidLoad() { super.viewDidLoad() // ໰୊ͷಡΈࠐΈ

    csvArray = loadCSV(filename: "loveswift") // ը໘ͷॳظԽ answerImage.isHidden = true // ໰୊දࣔ nextProblem() }