[Swift] Object | Structure | Data , Property , Method
Object = Data + Method
object는 struct나 class 형태로 구현할 수 있다.
이곳에서는 struct로 구현한다.
struct Store {
/****************************************************** 데이터 = 프로퍼티 (Stored Property) */
let loc: Location
var name: String
let deliveryRange = 2.0
/*************************************************************/
/****************************************************** 메소드 */
func isDeliverable(userLoc: Location)->Bool {
let distanceToStore = distance(current)
return distanceToStore < deliveryRange
}
/*************************************************************/
}
struct Lecture: CustomStringConvertible {
/************************************************************ Computed Property */
var description: String {
return "Title: \(name), Instructor: \(instructor)"
}
/*************************************************************/
/*************************************************************/
let name: String
let instructor: String
let numOfStudent: Int
/*************************************************************/
}
Data = Property
Sroted Property = 변수 저장해서 갖고 있는 애
Computed Property = 기존 저장된 값 가공해서 씀. 어떤 값을 직접 저장하진 않고, 저장된 정보를 이용해서 가공/계산된 값을 제공할 때 사용. 접근할때마다 값이 변경되서 값이 변경되면 새로운 저장된 값을 이용해서 computed property를 다시 만듦. set 시킬때는 자기 스스로가 저장되는게 아니라 자기가 받은 값 갖고 stored property에 나눠서 설정.
Type Property = 생성된 인스턴스에 상관없이 struct/class의 타입 자체의 속성을 정하고 싶을 때 사용.
Lazy Property = 인스턴스가 생성될 때 실행된다기 보다, 프로퍼티가 접근될 때 그제서야 코드가 실행되는 프로퍼티. 최적화하기 위해 사용.
Property Vs Method
Property = 호출 시 저장된 값을 하나 반환한다.
Method = 호출 시 어떤 작업을 한다.
--> Method가 그냥 값을 리턴하는 작업을 한다면 ?
stackoverflow.com/questions/24035276/computed-read-only-property-vs-function-in-swift
Computed read-only property vs function in Swift
In the Introduction to Swift WWDC session, a read-only property description is demonstrated: class Vehicle { var numberOfWheels = 0 var description: String { return "\(numberOfWhee...
stackoverflow.com
medium.com/swift-india/functions-vs-computed-property-what-to-use-64bbe2df3916
Functions vs Computed property — What to use?
Many times while development we get caught in a situation on what to use?
medium.com
softwareengineering.stackexchange.com/questions/304077/swift-functions-vs-computed-properties
Swift functions vs computed properties
Say I have have a class Event as follows: class Event { private var attendees: [Person] = [] // Case 1 //******* // Should I use a func… func countOfAttendees() -> Int { ...
softwareengineering.stackexchange.com
위에서 대부분 복잡도와 쓰로우, 계산 빠르기 등을 다루는 것 같다.
모호하다......애매모호...