전체 글(24)
-
[ Swift ] Tuples
// 0 1 let studentMark: (String, Int) = ("Chris", 46) studentMark.0 // "Chris" studentMark.1 // 46 //012 let studentData = (name: "Chris", mark: 46, petName: "Mango") let theName = studentData.name let theMark = studentData.mark let thePetName = studentData.petName let (name, mark, petName) = studentData //
2020.10.06 -
[ Swift ] Optional
var petName: String? petName = "Mango" print(petName) petName 변수를 String 또는 nil 값으로 처음에 선언했다. petName = "Mango" 로 선언했다 . petName 을 출력하면 wrap 된 값이 출력되므로 Optional("Mango") 다음과 같은 문구가 출력된다. var petName: String? petName = "Mango" print(petName) petName = nil//unwrapping optional var result: Int? = 30 print(result) petName 을 nil 로 unwrapping 시켜준다. result 값을 Int 형 Optional 값으로 선언하고 출력을 하면 Optional("30..
2020.10.05 -
[ Swift ] if, guard, switch문 사용방법
1. if 2. guard 3. switch 1 - if if{ //... } 해당 조건에 맞으면 블럭내의 구문(//...)을 실행합니다. else if, else 문 사용방법 또한 다른언어와 유사하기 때문에 이정도 정리하고 넘어가겠습니다. 2 - guard guard else{ } 형식으로 작성된다. if 문과의 차이점으로는 else구문이 반드시 필요하다는 점. 참일때의 실행코드블럭이 없다. guard구문은 후속 코드가 실행되기 전 조건을 만족하는지 확인하는 용도로 쓰입니다. 특정 조건을 만족하지 않고 후속코드를 실행하지 못하도록 오류발생을 예방하는 코드로 사용됩니다. 장점 1> 코드의 중첩을 막아준다. 2> guard구문을 많이 사용해도 코드의 depth가 깊어지지 않는다. import Foundat..
2020.09.29