티스토리 뷰

var str = "Hello, playground"


// If

if str != "Hello, playground" {

    print(true)

} else{

    print(false)

}


// For

// deprecated: 강의에는 가능하다고 되어있지만 playground에서 실행하니까 deprecated되었다고 경고가 떴다. 아래와 같이 수정해야한다. 

for var index = 0; index < 3; index += 1 {

    print("index is \(index)")

}


// fixed

for index in 0..<3{

    print("index is \(index)")

}


// range expression: n...m 값의 범위를 나타낸다.

for var index in 0...3 {

    print("index is \(index)")

}


var arr:Array<String> = ["test1","test2","test3"]

for item in arr{

    print("item \(item)")

}



// dictionary 경우 for문에서 tuple 사용하여 받는다.

var dictionary:[String:Int]  = ["test1":1,"test2":2,"test3":3, "test5":5, "test10":10, "test20":20]

for (key,value) in dictionary{

    // switch 문은 기존의 switch 다르게 range expression(n...m) where절을 이용한 조건 구문을 작성 있다.

    // where 절은 보통 변수 선언 + 조건문으로 구성된다(var temp where temp > 30).

    // 만약 변수 선언이 필요 없을 경우 변수 대신 _ 사용한다.

    var result:String

    switch(value){

    case 1:

        result = "\(key) 1";

    case 2...5:

        result = "\(key) 2 5 사이"

    case _ where value > 10:

        result = "\(key) 10보다 크다"

    default:

        result = "\(key) Default"

    }

    print("result: \(result)")

}



// 출력 결과 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/05   »
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
글 보관함