전체 글
[swift_07]함수
함수 void형 함수 func hello(){ print("Hello") } 반환하는 타입이 없음 Return func hello2() -> String{ return "Say Hello" } 반환하는 타입 : String Parameter func add(a:Int, b:Int) -> Int{ return a + b } add(a:3 , b:4) parameter와 argument label이 동일 Default parameter func add2(a:Int = 3, b:Int) -> Int{ return a + b } a 파라미터에 기본값 3, 만약 argument a에 대입이 없으면 a = 3 Argument Label func add3(a first:Int, _ second:Int) -> Int{..
[swift_06]조건문 if문과 switch문
if문 if문 선언과 사용 방법 let age = 7 if age = 3 && age < 20{ print("Child") } else{ print("Adult") } 일반적으로 사용하는 if문과 동일함 Switch문 switch문 사용하는 방법 switch age { case 0,1,2 : print("Baby") case 3...19 : print("Child") default : print("Adult") } switch문은 기존에 내가 알고 있던 switch에 비해 상당히 강력함. ,를 활용하여 여러가지 조건에서 확인할 수 있음. where절을 활용하여 추가적인 조건을 줄 수 있음 break문이 필요 없음
[swift_05]반복문 while과 for문
while문 while문 선언과 사용 var index = 5 while index < 0{ index -= 1 print(index) } For문 배열에서 사용하는 법 let names = ["a","b","c"] for name in names { print(name) } python의 for-in 동일 사전에서 활용하는 let a = "a" let b = " b" var first = a + b var second = "c" second += first // ca b 숫자 범위로 사용하기 for index in 1...5{ print("\(index) times 5 is \(index * 5)") } for _ in 1...5{ print("Hello") } stride 활용해서 사용하기 let m..
[swift_04]Collection-Dictionary
Dictionary Mutable Dictionary 생성 var dictionary = Dictionary() var dictionary2 = [String:Int]() Dictionary에 추가 dictionary2["and"] = 6 dictionary2["snake"] = 0 []괄호 안에 key, 그리고 assignment뒤에 value Dictionary 초기화 dictionary3 = ["and":6,"snake":0,"cat":4] :을 활용하여 key와 value 구분하여 생성 value 변경 dictionary3["cat"] = 5 기존에 있는 key값에 새로운 값을 assign immutable Dictionary 생성 let dictionary4 = ["ant":3,"snake":0..