Swift/문법

Structure / Protocol

khon98 2021. 3. 2. 23:20

Structure와 Class의 차이

- Structure는 Value 타입, Class는 Reference타입

 

Structure

import UIKit

// Structure

// 내 위치에서 가까운 스토어 찾기

// 거리구하는 함수
// func distance(current: (x: Int, y: Int), target: (x: Int, y: Int)) -> Double {
func distance(current: Location, target: Location) -> Double {
    let distanceX = Double(target.x - current.x)
    let distanceY = Double(target.y - current.y)
    let distance = sqrt(distanceX * distanceX + distanceY * distanceY)
    return distance
}

struct Location {
    let x: Int
    let y: Int
}

struct Store {
    let loc: Location
    let name: String
    let deliveryRange = 2.0
    
    func isDeliverable(userLoc: Location) -> Bool {
        let distanceToStore = distance(current: userLoc, target: loc)
        return distanceToStore < deliveryRange
    }
}

// 현재 스토어 위치
let store1 = Store(loc: Location(x: 3, y: 5), name: "cu")
let store2 = Store(loc: Location(x: 4, y: 6), name: "gs")
let store3 = Store(loc: Location(x: 1, y: 7), name: "seven")

// 가장 가까운 스토어 구해서 프린트 하는 함수
// func printStore(currentLocation:(x: Int, y: Int), stores:[(x: Int, y: Int, name: String)]) {
func printStore(currentLocation: Location, stores: [Store]) {
    var storeName = ""
    var storeDistance = Double.infinity
    var isDeliverable = false
    
    for store in stores {
        let distanceToStore = distance(current: currentLocation, target: store.loc)
        storeDistance = min(distanceToStore, storeDistance)
        if storeDistance == distanceToStore {
            storeName = store.name
            isDeliverable = store.isDeliverable(userLoc: currentLocation)
        }
    }
    print("store: \(storeName), isDeliverable: \(isDeliverable)")
}

// stores array 세팅, 현재 내 위치 세팅
let myLocation = Location(x: 2, y: 2)
let stores = [store1, store2, store3]

// 현재 가장 가까운 스토어 출력
printStore(currentLocation: myLocation, stores: stores)






// 강의 이름, 강사 이름, 학생 수를 가지는 struct 생성
struct Lecture {
    let name: String
    let Instructor: String
    let Student: Int
}

// 강의 array와 강사 이름을 받아서, 강사의 강의 이름을 출력하는 함수 생성
func printLecture(from instructor: String, lectures: [Lecture]) {
     var lectureName = ""

     for lecture in lectures {
         if instructor == lecture.Instructor {
             lectureName = lecture.name
         }
     }
    
//    let lectureName = lectures.first { $0.Instructor == instructor }?.name ?? ""
    print("강사님 강의는 \(lectureName)입니다")
}

// 강의 3개 만들고 강사 이름으로 강의 찾기
let lec1 = Lecture(name: "ios", Instructor: "khon", Student: 5)
let lec2 = Lecture(name: "android", Instructor: "khon01", Student: 3)
let lec3 = Lecture(name: "Reading Book", Instructor: "khon02", Student: 1)
let lectures = [lec1, lec2, lec3]

printLecture(from: "khon", lectures: lectures)

 

 

Protocol

- 지켜야 할 약속

- 서비스를 이용해야 할 때 할 일들의 목록

- CustomStringConvertible 프로토콜을 정의하면 사용자가 정의한 형태로 출력

import UIKit

// Protocol
// 강의 이름, 강사 이름, 학생 수를 가지는 struct 생성
struct Lecture: CustomStringConvertible {
    var description: String {
        // 원하는 lec을 입력하면 해당 강의 이름과 강사 명 출력
        return "Title: \(name), Instructor: \(Instructor)"
    }
    let name: String
    let Instructor: String
    let Student: Int
}

// 강의 array와 강사 이름을 받아서, 강사의 강의 이름을 출력하는 함수 생성
func printLecture(from instructor: String, lectures: [Lecture]) {
     var lectureName = ""

     for lecture in lectures {
         if instructor == lecture.Instructor {
             lectureName = lecture.name
         }
     }
    
//    let lectureName = lectures.first { $0.Instructor == instructor }?.name ?? ""
    print("강사님 강의는 \(lectureName)입니다")
}

// 강의 3개 만들고 강사 이름으로 강의 찾기
let lec1 = Lecture(name: "ios", Instructor: "khon", Student: 5)
let lec2 = Lecture(name: "android", Instructor: "khon01", Student: 3)
let lec3 = Lecture(name: "Reading Book", Instructor: "khon02", Student: 1)
let lectures = [lec1, lec2, lec3]

printLecture(from: "khon", lectures: lectures)
print(lec1)