Toy Project 81

8. Swift Structure

// Structure: 관계가 있는 것들을 묶어서 표현한 것 // Structure와 Class는 동작이 다르다. // 구조체는 Value Types으로 변수에 할당될 때 Copy되어 할당된다. // 클래스는 Reference Types으로 변수에 할당될 때 참고되는 형태(공유)로 할당된다. // Structure는 Stack에 할당되고, Class는 Heap에 할당이 된다. // let pClass1 = PersonClass(name: "me") // let pClass2 = pClass1 // pClass2.name = "Hey" // pClass1.name // Hey // pClass2.name // Hey // let pStructure1 = PersonStruct(name: "me") // ..

7. Swift Closure 조금더

// Closure 보강!!!! // 함수는 어떤 기능을 수행하는 코드블록이며, // Swift에서는 함수와 같이 기능을 수행하는 코드블록의 특별한 타입인 Closure가 존재한다. 사실 Closure가 더 상위 개념이다 // Swift 공식사이트에서 정의한 Closure: // Closure는 크게 3가지 타입이 존재한다. // Global functions are closures that have a name and don’t capture any values. // Nested functions are closures that have a name and can capture values from their enclosing function. // Closure expressions are unna..

6. Swift Closure basic

// Closure : 이름이 없는 메소드라고 생각하면 된다. // (Int, Int) -> Int : 클루져의 타입 var multiply1: (Int, Int) -> Int = { (a:Int, b:Int) -> Int in return a * b } // 여기서 간략하게 쓸 수 있다. // a, b는 Int로 들어올 것이고, 반환도 Int로 한다고 type을 지정했다. var multiply2: (Int, Int) -> Int = { a, b in return a * b } // 그리고 들어오는 파라미터의 index를 나타낼 수 있다. var multiply3: (Int, Int) -> Int = { return $0 * $1 } var multiplyClosure: (Int, Int) -> Int..

5. Swift Collection(Dicionary, Set)

// Dictionary // 순서가 없이 key와 value로 구성된다. // 커피에 이름을 적는 것과 비슷한 구조이다. // Key는 Unique해야한다. var scoreDic: [String: Int] = ["Jason": 80, "Jay": 95, "Jake": 90] var scoreDic1: Dictionary = ["Jason": 80, "Jay": 95, "Jake": 90] print(scoreDic) print(scoreDic["Jay"]) // Optional type이다. 없을 수 있기때문 print(scoreDic["duddn"]) // nil 값이 나온다. if let score = scoreDic["Jay"] { print(score) } // isEmpty, count pr..

4. Swift Collection (Array)

// Collection 컬렉션, 수집 ... // 수많은 변수를 하나의 통에 넣어 관리하면 편하다 // 이때 통에서 순서를 두어 커피를 나눠줄 수 있다. // 커피에 이름이 새겨 구분하여 나눠줄 수 있다. // Array, Dictionary, Set, Closure // Array : 동일한 타입, 순서index var evenNumbers: [Int] = [2, 4, 6, 8] var evenNumbers2: Array = [2, 4, 6, 8] evenNumbers.append(10) //let이면 오류뜬다. print(evenNumbers) evenNumbers += [12, 14, 16] print(evenNumbers) evenNumbers.append(contentsOf: [18, 20])..

3. Swift Optional

//Optional: 있는 것과 없는 것을 모두 표현하기 위함 // 코드 중간에 ?, !가 들어가서 이상해 보일 수 있다. // var name: String = "Joon" // 없는 것에 대해 표현하기 애매하다. 없는 것을 어떻게 표현할 것인가!! // var dogName: String = ??? // 존재하지 않음을 표현 : nil(없을 무), None과 유사 var carName: String? //Optional 변수의 표현 //여러분이 최애하는 영화배우 이름을 담는 변수 작성하기, 타입:String //let num = Int("10") 에서 num의 type는? var favoriteActor: String? = nil // 없을 수 있다. print(favoriteActor) //여기서 ..

2. Swift Function

//object의 멤버함수를 method, 전역함수를 function라고 한다. func printTotalPrice(price: Int, count: Int){ print("1. \(price*count)") } printTotalPrice(price: 1500, count: 5) //이렇게 parameter를 지정해 주어야한다. //지정안하고 보내는 방법이 존재한다. //어떻게? parameter 앞에 _를 붙힌다. //parameter의 이름을 지정할 수 있는 형식이다. //"가격"은 밖에서만, "price"는 함수 내에서만 사용가능!!!! func printTotal1(_ price: Int, _ count: Int){ print("2. \(price*count)") } printTotal1(15..