티스토리 뷰
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)")
}
// 출력 결과
'Programming > iOS' 카테고리의 다른 글
[iOS/Swift] Auto Layout Without Constraints 번역 (0) | 2016.10.01 |
---|---|
[iOS/Swift] Understanding Auto Layout 번역 (0) | 2016.10.01 |
[IOS/Swift] Collections - Dictionary / Set (0) | 2016.08.07 |
[IOS/Swift] Collections - Array (0) | 2016.08.07 |
[IOS/Swift] Tuple, Typealias (0) | 2016.08.07 |