꾸준히 듣고있진 않지만 그래도 다행히 몰아서라도 들으며 강의는 듣고있는 내가 참 대견하다.
사실 나는 위에 보이는 화면을 직접 구성하는 그런 작업을 해보고 싶은데 아직까지는 구문, 문법같은 내용을 기초로 쌓아가는 중이라 진도를 빨리 빼고 싶다는 생각이 들기도한다.
하지만 진도를 빨리 빼기에는 직장생활을 병행하며 출퇴근시간에 듣는 방법과 주말에 듣는 방법이 있는데 출퇴근시간에 이 수업을 듣게된다면 듣기만하고 직접 해볼 수 있는 기회가 없는 상태이기 때문에 거의 주말에 몰아서 듣게되는 것 같다.
출퇴근 시간이 짧기라도 하면 상관없을텐데 너무 길다.
이번 2주차에 배운 내용으로는 구조체, 프로토콜, 프로퍼티, 메소드, 클래스, 스트럭트, 상속, 생성자 총 8개 정도의 내용에 대해 배웠다.
먼저 구조체는 인스턴스의 값을 저장하거나 캡슐화할 수 있는 타입이다.
코드를 통해 예를 들자면
// 스토어의 위치들
let store1 = (x: 3, y: 5, name: "gs")
let store2 = (x: 4, y: 6, name: "seven")
let store3 = (x: 1, y: 7, name: "cu")
// 함수 - 거리구하기
func distance(current: (x: Int, y: Int), target: (x: Int, y: Int)) -> Double {
let distanceX = Double(target.x - current.x)
let distanceY = Double(target.y - current.y)
let distance = sqrt(distanceX * distanceX + distanceY * distanceY)
return distance
}
// 함수 - 가장 가까운 스토어 찾아서 프린트하기
func printClosestStore(currentLocation: (x: Int, y: Int), stores: [(x: Int, y: Int, name: String)]) {
var closestStoreName = ""
var closestStoreDistance = Double.infinity
for store in stores {
let distanceToStore = distance(current: currentLocation, target: (x: store.x, y: store.y))
closestStoreDistance = min(distanceToStore, closestStoreDistance)
if closestStoreDistance == distanceToStore {
closestStoreName = store.name
}
}
print("Closest store: \(closestStoreName)")
}
// Stores의 Array와 현재 내 위치 세팅
let stores = [store1, store2, store3]
let myLocation = (x: 2, y: 2)
// printClosestStore 함수를 이용하여 현재 가장 가까운 스토어 출력하기
printClosestStore(currentLocation: myLocation, stores: stores)
위의 코드는 현재 자신의 위치에서 가장 가까운 매점을 찾는 코드이다.
위에서 계속 생성되는 "(x: Int, y: Int)" 와 "(x: Int, y: Int, name: String)"를 각각 Location과 Store라는 구조체로 묶으면 아래의 코드처럼 나오게된다.
//위의 코드 중 x,y 좌표값을 Location 이라는 구조체로 묶은 경우이다
struct Location {
let x: Int
let y: Int
}
struct Store {
let loc: Location
let name: String
}
// 스토어의 위치들
let store1 = (loc: Location(x: 3, y: 5), name: "gs")
let store2 = (loc: Location(x: 4, y: 6), name: "seven")
let store3 = (loc: Location(x: 1, y: 7), name: "cu")
// 함수 - 거리구하기
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
}
// 함수 - 가장 가까운 스토어 찾아서 프린트하기
func printClosestStore(currentLocation: Location, stores:[(loc: Location, name: String)]) {
var closestStoreName = ""
var closestStoreDistance = Double.infinity
for store in stores {
let distanceToStore = distance(current: currentLocation, target: store.loc)
closestStoreDistance = min(distanceToStore, closestStoreDistance)
if closestStoreDistance == distanceToStore {
closestStoreName = store.name
}
}
print("Closest store: \(closestStoreName)")
}
// Stores의 Array와 현재 내 위치 세팅
let stores = [store1, store2, store3]
let myLocation = Location(x: 2, y: 2)
// printClosestStore 함수를 이용하여 현재 가장 가까운 스토어 출력하기
printClosestStore(currentLocation: myLocation, stores: stores)
이렇듯 구조체를 이용할경우 반복되는 코드들을 간결하게 줄일 수 있기때문에 전체적으로 깔끔해질 것으로 보인다. 일단은 이번 글은 구조체로 마무리 하고 다음에 이어서 진행해보겠다.