20일차 - 21.06.27
학습키워드
- array shuffling
- random number generation
- @IBAction
- UIAlertController
1. Guess which flag: random numbers
배열 순서를 섞는 방법
배열 순서를 섞는 방법
shuffle()
: 배열 원본 자체를 무작위로 순서를 섞는 메서드
shuffled()
: 무작위로 순서를 섞은 배열의 복사본을 반환하는 메서드
스위프트의 숫자와 같은 유형에 난수를 생성하는 방법
Int, Double, CGFloat 등등
숫자와 같은 유형에서 난수를 생성하려면
random(in:)
메서드를 사용합니다.
- 예시
Int.random(in: 0...3)
Double.random(in: 0...3)
CGFloat.random(in: 0...4)
Float.random(in: 0...2)
2. From outlets to actions: creating an IBAction
@IBAction : 인터페이스빌더 요소가 이벤트가 발생했을 때 처리할 수 있도록 하는 것
TouchUpInside
: 사용자가 버튼을 터치한 다음 손가락을 뗀 방식
태그라는 특별한 식별 번호를 통해 요소들을 구분할 수 있습니다.
@IBAction func buttonTapped(_ sender: UIButton) {
var title: String
if sender.tag == correctAnswer {
title = "Correct"
score += 1
} else {
title = "Wrong"
score -= 1
}
}
경고창 표시
UIAlertController() : 사용자에게 옵션과 함께 경고를 표시하는데 사용되는 타입
-
예제코드
let ac = UIAlertController(title: title, message: "Your score is \(score).", preferredStyle: .alert) ac.addAction(UIAlertAction(title: "Continue", style: .default, handler: askQuestion)) present(ac, animated: true)
- UIAlertController의 title: 제목 message : 메세지 상자, preferredStyle : 보여지는 스타일 .actionSheet와 .alert 둘 중 선택
- UIAlertAction 경고창에 버튼을 추가할 때 사용하는 타입 스타일은 .default, .cancel, .destructive 등이 있습니다. handler에는 액션을 선택했을 때 원하는 동작을 지정합니다.
링크
100 Days of Swift - Day 20 - Hacking with Swift
Author And Source
이 문제에 관하여(20일차 - 21.06.27), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@sookim-1/20일차-21.06.27저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)