티스토리 뷰
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
- Add Slider
- Add 'HIT Button'
- Receive value changed events from the slider
- Add 'RESET Button'
- Add labels presentin information
- Generate the random number
- Compare the random number with input number
- Show alerts
- Implement 'reset' feature
- Add 'Credit' view
'iOS' 카테고리의 다른 글
오토레이아웃 정복하기#1 (0) | 2021.04.01 |
---|---|
[iOS] 왕초보를 위한 iOS 앱개발 6 (0) | 2021.03.15 |
[iOS] 왕초보를 위한 iOS 앱개발 4 (0) | 2021.03.12 |
[iOS] 왕초보를 위한 iOS 앱개발 3 (0) | 2021.03.12 |
[iOS] 왕초보를 위한 iOS 앱개발 1~2 (0) | 2021.03.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 흐름제어구문
- optional
- 결합성
- enumerations
- 오버플로우연산자
- conditional
- Functions
- 옵셔널
- Constants
- 반복문
- 훈련법
- labeled
- 전산구문 기초용어
- 오토레이아웃
- playground
- 객체지향 생활체조
- 범위연산자
- iOS View Life Cycle
- 구문이름표
- 세자리수마다 콤마넣기
- RawValues
- variables
- 산술연산자
- continue
- scope
- overflow
- 삼항연산자
- datatypes
- swift
- 비트연산자
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함