티스토리 뷰

iOS

[iOS] 왕초보를 위한 iOS 앱개발 5

learner._.Kio 2021. 3. 12. 09:45
 

왕초보를 위한 iOS 앱개발 - 야곰닷넷

코딩의 '코'자를 몰라도 시작해 볼 수 있는 iOS 앱만들기! 왕초보를 위한 iOS 앱개발 입문편입니다.

yagom.net

5. 코드로 기능 구현하기

Lesson5 Topics

  • Changing value to another type
  • String Interpolation
  • Comparing Values
  • Challenge - Comparing Values
  • Terminating functions
  • Conditional execution
  • Showing Alerts
  • Wrapping Up

5-1. 값을 다른 타입의 변수에 전달하기

  • 서로 다른 변수의 값은 저장할 수 없다.
    • Int != Double
    @IBAction func sliderValueChanged(_ sender: UISlider) {
        print(sender.value)
        let integerValue: Int = Int(sender.value)
        sliderValueLabel.text = String(integerValue)
    }

    @IBAction func touchUpHitButton(_ sender: UIButton) {
        print(slider.value)
        let hitValue: Int = Int(slider.value)
        slider.value = Float(hitValue)
    }

5-2. 원하는 문자열 만들기 – 문자열 보간법

문자열 보간법

  • 문자열에 실제 변수를 넣어 출력하는 방법
    @IBAction func touchUpHitButton(_ sender: UIButton) {
        print(slider.value)
        let hitValue: Int = Int(slider.value)
        slider.value = Float(hitValue)

        tryCount = tryCount + 1
        tryCountLabel.text = "\(tryCount) / 5" // 문자열 보간법
    }

5-3. 값을 비교해보기

     if randomValue == hitValue {
         print("YOU HIT!!")
     }
  • ==, <=, >=, !=

5-4. [도전!] 조건에 따른 실행

      if tryCount >= 5 {
          print("You lose...")
      }

5-5. 논리적 오류 해결하기 / 함수 끝내기

return

      if randomValue == hitValue {
          print("YOU HIT!!")
          reset()
          return // 함수를 여기에서 종료하겠다.
      }

      if tryCount >= 5 {
          print("You lose...")
          reset()
          return
      }

5-6. 조건문의 조건추가

      if randomValue == hitValue {
          print("YOU HIT!!")
          reset()
      } else if tryCount >= 5 {
          print("You lose...")
          reset()
      } else if randomValue > hitValue {
          slider.minimumValue = Float(hitValue)
          minimunValueLabel.text = String(hitValue)
      } else {
          slider.maximumValue = Float(hitValue)
          maximunValueLabel.text = String(hitValue)
      }

5-7. 사용자에게 얼럿 보여주기

    func showAlert(message: String) {

        let alert = UIAlertController(title: nil,
                                      message: message,
                                      preferredStyle: .alert)
        let okAction = UIAlertAction(title: "OK",
                                     style: .default) { (actions) in
                                        self.reset()
        }

        alert.addAction(okAction)
        present(alert,
                animated: true,
                completion: nil)
    }

5-8. 레슨5 정리

Wrapping Up

  • Changing value to another type
  • String Interpolation
  • Comparing Values
  • Challenge - Comparing Values
  • Terminating functions
  • Conditional execution
  • Showing Alerts
  • Wrapping Up

To-Do List

  1. Add Slider
  2. Add 'HIT Button'
  3. Receive value changed events from the slider
  4. Add 'RESET Button'
  5. Add labels presentin information
  6. Generate the random number
  7. Compare the random number with input number
  8. Show alerts
  9. Implement 'reset' feature
  10. Add 'Credit' view
댓글