티스토리 뷰
Swift type properties
- What
You can also define properties that belong to the type itself, not to any one instance of that type. There will only ever be one copy of these properties, no matter how many instances of that type you create. These kinds of properties are called type properties.
type 프로퍼티는 타입 그 자체에 속해 있는 것이다. 타입인스턴스가 아니라 또한 이 type properties의 복사본은 하나밖에 존재 하지 않는다. 여러분이 얼마나 많은 인스턴스를 만드는지 상관 없이. 이런한 것들은 type properties라고 부른다
-> 타입 프로퍼티는 타입에 고유한 것입니다. 그 타입의 인스턴스를 만든다고 새로 생성되지 않습니다. 하나의 예를 들면 Double.pi가 있습니다.
public static var pi: Double { get }
-> pi는 Double타입 그 자체에 속해 있습니다. 위와 같이 말이죠. read-olny computed로 선언 돼 있습니다. 고유한 값이라고 해도 될 것 같에요. 바뀌지 않는... 왜냐면 Dobule.pi를 하면 어디서든 3.14... 값을 얻기 위함이죠. constant변수로 선언해도 될 것 같네요.
- instance와 type 프로퍼티의 차이
Instance properties are properties that belong to an instance of a particular type. Every time you create a new instance of that type, it has its own set of property values, separate from any other instance
인스턴스 프로퍼티는 속성입니다. 인스턴스의 특정타입의
매번 타입의 새로운 인스턴스를 만들 때마다, 인스턴스는 자신의 프로퍼티 값을 가집니다. 다른 인스턴스들과 분리돼서
-> 인스턴스변수는 특정 인스턴스에 속해있는 값인 반면, 타입프로퍼티는 특정 인스턴스에 속해 있지 않고 모든 인스턴스에서 공용으로 사용 가능합니다.
-> 객체를 설계할 때 모든 인스턴스에 공통된 값을 유지해야하는 프로퍼티가 있다면 static을 붙이기
-> 객체의 메서드 중에서 인스턴스 변수나 인스턴스 메소드를 사용않는 메서드에 static을 붙이기
-> 그리고 타입 메서드나 type computed property에서 인스턴스 변수를 사용할 수 없다
-> type method나 type computed property는 인스턴스 생성 없이도 사용할 수 있지만
-> 인스턴스 변수는 인스턴스를 생성해야하기 때문
-> 반대의 경우는 언제나 가능 인스턴스가 생성됐다는 것은 타입 메서드나 type computed property가 존재 한다는 걸 의미하기 때문
- When
'Swift & objc' 카테고리의 다른 글
swift unit test (0) | 2018.03.19 |
---|---|
피드백 정리 (0) | 2018.03.19 |
typealias, Tuple (0) | 2018.03.16 |
swift guard, if 기준정하기 (0) | 2018.03.14 |
swift protocols (0) | 2018.03.14 |