存储属性
import UIKit
// 存储属性
struct FixedLengthRange {
var firstValue: Int
var length: Int
}
var rangeOfThreeItems = FixedLengthRange(firstValue: 0, length: 3)
rangeOfThreeItems.firstValue = 6
let rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
// 结构体常量的属性值是不可以修改的,会报错,如下
// rangeOfFourItems.firstValue = 6
懒加载的存储属性
// 懒加载的存储属性
class DataImporter {
var fileName = "data.text"
}
class DataManager{
lazy var importer = DataImporter()
var data = [String]()
}
let manager = DataManager()
manager.data.append("Some data")
manager.data.append("Some more data")
print(manager.importer.fileName)
console log 如下
懒加载的存储属性.png
计算属性
// 计算属性
struct Point {
var x = 0.0, y = 0.0
}
struct Size {
var width = 0.0, height = 0.0
}
struct Rect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + size.width / 2.0
let centerY = origin.y + size.height / 2.0
return Point(x: centerX, y: centerY)
}
set(newCenter) {
origin.x = newCenter.x - size.width / 2.0
origin.y = newCenter.y - size.height / 2.0
}
}
}
var square = Rect(origin: Point(x: 0, y: 0), size: Size(width: 10, height: 10))
let initialSquareCenter = square.center
print("square center is now at (\(initialSquareCenter.x), \(initialSquareCenter.y))")
square.center = Point(x: 15, y: 15)
print("square origin is now at (\(square.origin.x), \(square.origin.y))")
// 计算属性,设置方法使用默认参数
struct AlternativeRect {
var origin = Point()
var size = Size()
var center: Point {
get {
let centerX = origin.x + size.width / 2.0
let centerY = origin.y + size.height / 2.0
return Point(x: centerX, y: centerY)
}
set {
origin.x = newValue.x - size.width / 2.0
origin.y = newValue.y - size.height / 2.0
}
}
}
console log 如下
计算属性.png
只读的计算属性
// 只读的计算属性
struct Cuboid {
var width = 0.0, height = 0.0, depth = 0.0
var volume: Double{
return width * height * depth
}
}
let fourByFiveByTwo = Cuboid(width: 4, height: 5, depth: 2)
print("the volume of fourByFiveByTwo is \(fourByFiveByTwo.volume)")
console log如下
只读的计算属性.png
观察属性
// 观察属性
class StepCounter {
var totalSteps: Int = 0 {
willSet (newTotalSteps) {
print("About to set totalSteps to \(newTotalSteps)")
}
didSet (oldTotalSteps) {
if totalSteps > oldTotalSteps {
print("Add \(totalSteps - oldTotalSteps) steps")
}
}
}
}
let stepCounter = StepCounter()
stepCounter.totalSteps = 100
stepCounter.totalSteps = 80
// 观察属性使用默认参数
class AnotherStepCounter {
var totalSteps: Int = 0 {
willSet {
print("About to set totalSteps to \(newValue)")
}
didSet {
if totalSteps > oldValue {
print("Add \(totalSteps - oldValue) steps")
}
}
}
}
let anotherStepCounter = AnotherStepCounter()
anotherStepCounter.totalSteps = 85
anotherStepCounter.totalSteps = 520
console log 如下
观察属性.png
static 关键字
// static 关键字
struct SomeStructure {
static var storedTypeProperty = "Some value."
static var computedTypeProperty: Int {
return 1
}
}
print(SomeStructure.storedTypeProperty)
SomeStructure.storedTypeProperty = "Another Value."
print(SomeStructure.storedTypeProperty)
struct AudioChannel {
static let thresholdLevel = 10
static var maxInputLevelForAllChannels = 0
var currentLevel: Int = 0 {
didSet {
if currentLevel > AudioChannel.thresholdLevel {
currentLevel = AudioChannel.thresholdLevel
}
if currentLevel > AudioChannel.maxInputLevelForAllChannels {
AudioChannel.maxInputLevelForAllChannels = currentLevel
}
}
}
}
var leftChannel = AudioChannel()
var rightChannel = AudioChannel()
leftChannel.currentLevel = 7
print(leftChannel.currentLevel)
print(AudioChannel.maxInputLevelForAllChannels)
rightChannel.currentLevel = 11
print(rightChannel.currentLevel)
print(AudioChannel.maxInputLevelForAllChannels)
console log 如下
static 关键字.png