1월 17일 (수)
Access Stored and computed values that are part of an instance or type
인스턴스 또는 타입의 한 부분이고, 저장 그리고 연산의 값에 접근할 수 있다고 한다.
Computed properties are provided by classes, structures, and enumerations. Stored properties are provided only by classes and structures.
Computed properties(연산프로퍼티)는 클래스, 구조체, 열거형에 사용할 수 있고, Stored properties(저장프로퍼티)는 오직 클래스와 구조체와만 사용할 수 있다.
[Stored Properties Example]
struct FixedLengthRange {
var firstValue: Int
let length: Int
}
var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
// the range represents integer values 0, 1, and 2
rangeOfThreeItems.firstValue = 6
// the range now represents integer values 6, 7, and 8
rangeOfThreeItems.length = 4 // Cannot assign to property: 'length' is a 'let' constant 라는 메시지 발생
[lazy Stored Properties Example]
class DataImporter {
/*
DataImporter is a class to import data from an external file.
The class is assumed to take a nontrivial amount of time to initialize.
*/
var filename = "data.txt"
// the DataImporter class would provide data importing functionality here
}
class DataManager {
lazy var importer = DataImporter()
var data: [String] = []
// the DataManager class would provide data management functionality here
}
let manager = DataManager()
manager.data.append("Some data")
manager.data.append("Some more data")
// the DataImporter instance for the importer property hasn't yet been created
print(manager.importer.filename) // 지연 저장 프로퍼티 사용
https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties/