Toy Project/Swift Language Syntax

8. Swift Structure

개발자킹콩 2021. 1. 21. 16:42

// 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")
// let pStructure2 = pStructure1
// pStructure2.name = "Hey"

// pStructure1.name // me
// pStructure2.name // Hey

// 위와 같이 구조체는 복사된 값을 쓰기에 pStructure1과 pStructure2는 다른 데이터이다.
// 하지만 pClass1, pClass2는 클래스객체이기에 변수에 할당을 하게되면
// 같은 데이터를 참조하는 형태이기에 이러한 결과가 발생한다.



// import Foundation

// // 나와 가장 가까운 편의점 찾기
// 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)")
// }

// let myLocation = (x: 2, y: 2)
// let stores = [store1, store2, store3]
// printClosestStore(currentLocation: myLocation, stores: stores)

 

 

 

// 여기서 x, y는 관계있는 데이터이기에 구조체로 만들어 보자
import Foundation

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

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 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: "gs")
let store2 = Store(loc:Location(x: 4, y: 6), name: "seven")
let store3 = Store(loc:Location(x: 1, y: 7), name: "cu")

// 거리구하는 함수

 

func printClosestStore(currentLocation: Location, stores:[Store] ){
  var closestStoreName = ""
  var closestStoreDistance = Double.infinity
  var isDeliverable = false

 

  for store in stores{
    let distanceToStore = distance(current: currentLocation, target: store.loc)
    closestStoreDistance = min(distanceToStore, closestStoreDistance)
    if closestStoreDistance == distanceToStore {

      closestStoreName = store.name
      isDeliverable = store.isDeliverable(userLoc: currentLocation)
    }
  }
  print("Closest store: \(closestStoreName)", "isDeliverable: \(isDeliverable)")
}

 

let myLocation = Location(x: 2, y: 2)
let stores = [store1, store2, store3]
printClosestStore(currentLocation: myLocation, stores: stores)

'Toy Project > Swift Language Syntax' 카테고리의 다른 글

10. Swift Protocol  (0) 2021.01.21
9. Swift Structure practice  (0) 2021.01.21
7. Swift Closure 조금더  (0) 2021.01.21
6. Swift Closure basic  (0) 2021.01.21
5. Swift Collection(Dicionary, Set)  (0) 2021.01.21