1. Swift 3 : Control Flow
군산대학교 컴퓨터정보통신공학부 컴퓨터정보공학전공
남 광 우
kwnam@kunsan.ac.kr
Swift 3 Tour and Language Guide by Apple
꼼꼼한 재은씨의 Swift 2 프로그래밍
2. Control Flow
• Loop Statements
• for~ in ~ () {}
• while () {}
• repeat {} while()
• 조건문 Conditional Statements
• if ~ else ~
• switch
• guard ( ) else {}
• #available
• Control Transfer Statements
• break, continue
• fallthrough
• return
3. 흐름 제어 : For 구문
• for 구문의 구조
• ; 생략가능
for( 초기값; 비교값; 증가값)
{
{
for var i = 0; I < 10; i++
{
print( “ (i) 번째 실행“)
{
for (var i = 0; I < 10; i++)
{
print( “ (i) 번째 실행“);
{
4. For ~in ~구문
• for ~ in ~ 구문의 형태
• for ~ in 구문에서 루프상수의 생략
• 아래에서 변수 i는 필요없음
• 그러므로 _ 로 대체 가능
for i in 1…100
{
print( “Hello” )
}
for _ in 1…100
{
print( “Hello” )
}
for 루프상수 in 순회대상 {
실행할 구문들
}
{ 가 필수
5. For 구문
• for~ in 문으로 구현한 9*9단
for i in 1…9
{
for j in 1…9
{
print( “ (i) * (j) = (i*j) “);
}
}
( ) 치면 에러
6. For 구문
• 예 : String을 한 글자씩 출력하기
var str = “Swfit”
for c in str.characters {
print( c );
}
7. For 구문
• 예 : 배열의 내용을 하나씩 프린트 하기
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names
{
print("Hello, (name)!")
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!
8. For 구문
• 예 : dictionary의 내용을 프린트 하기
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs
{
print("(animalName)s have (legCount) legs")
}
// ants have 6 legs
// spiders have 8 legs
// cats have 4 legs
9. While 구문
• C와 동일
• while 문으로 구현한 9*9단
var i = 1;
while ( i<=9 )
{
var j = 1;
while ( j <= 9 )
{
print( “ (i) * (j) = (i*j) “);
j += 1;
}
i += 1;
}
i++ deprecated됨
10. repeat~ while 구문
• do~ while 구문의 변경
• do ~ try~ cactch와 keyword 겹침으로 인해 변경
• repeat~while
var n = 1024;
repeat {
n = n* 2;
} while n < 1000;
n// n== 2048
11. if 구문
• c의 구문과 동일
• 예 : 브라우저명을 입력받아 한글로 변환
var browser = “Safari”
var browserName : String;
if browser==“IE”
{
browserName = “인터넷 익스플로러“;
}
else if browser ==“FF”
{
browserName = “파이어폭스”;
}
12. switch 구문
switch 비교대상 {
case value 1:
value1의 작업
case value 2, value 3:
value2, value3의 작업
case value4:
fallthrough
case value5:
value4, value5의 작업
default:
otherwise, do something else
}
• c의 구문과 유사, 그러나
• break 없음
• case 두개 이상 적용할 경우 fallthrough
• 비교 대상은 꼭 하나이상의 case에 match 해야 함
• default 거의 필수
14. switch 구문
• 예 : 범위 연산의 사용
let approximateCount = 62
let countedThings = "moons orbiting Saturn"
var naturalCount: String
switch approximateCount {
case 0:
naturalCount = "no"
case 1..<5:
naturalCount = "a few"
case 5..<12:
naturalCount = "several"
case 12..<100:
naturalCount = "dozens of"
case 100..<1000:
naturalCount = "hundreds of"
default:
naturalCount = "many"
}
print("There are (naturalCount) (countedThings).")
// Prints "There are dozens of moons orbiting Saturn."
15. switch 구문
• 예 : Tuple의 사용과 value binding
let somePoint = (1, 2)
switch somePoint {
case (0, 0):
print("(0, 0) is at the origin")
case ( _, 0):
print("((somePoint.0), 0) is on the x‐axis")
case (0, let y):
print("(0, ( y )) is on the y‐axis")
case (‐2...2, ‐2...2):
print("((somePoint.0), (somePoint.1)) is inside the box")
default:
print("((somePoint.0), (somePoint.1)) is outside of the box")
}
// Prints "(1, 1) is inside the box"
_ 을 이용한
모든값
대응
(1,2) 튜플의 0번째값 1
somePoint.1은 2
y로 somePoint.1을 대체
17. switch 구문
• Where 구문의 사용
• 예 : where 를 이용한 직선의 기울기
let yetAnotherPoint = (1, ‐1)
switch yetAnotherPoint {
case let (x, y) where x == y:
print("((x), (y)) is on the line x == y")
case let (x, y) where x == ‐y:
print("((x), (y)) is on the line x == ‐y")
case let (x, y):
print("((x), (y)) is just some arbitrary point")
}
// Prints "(1, ‐1) is on the line x == ‐y"