Swift for iOS Development (9)-Properties

version

Xcode 11.3.1
Swift 5.1.3

Properties associate values ​​with specific classes, structures, or enumerations.

  • Store properties store constants and variables as part of the instance
  • Calculated attributes are directly calculated (rather than stored) values
  • Attributes can be directly associated with the type itself. Such attributes are called type attributes.

Storage attributes

struct Range {
    
    
    var min = 0.0
    var max = 0.0
}

let range = Range(min: 10.0, max: 20.0)
print("range.min = \(range.min), range.max = \(range.max)")
// range.min = 10.0, range.max = 20.0

Lazy load storage properties

Lazy loading uses the keyword lazy, so it is also known as lazy loading.
When the value of the attribute depends on some external factors and these external factors will only be known after the completion of the construction process, delayed loading of the attribute is useful. Or when obtaining the value of an attribute requires complex or a large amount of calculations and needs to be calculated when needed, delayed loading of the attribute will also be useful.

Note:
The delayed storage attribute must be declared as a variable (using the var keyword), because the value of the attribute may not be available before the instance is constructed. The constant attribute must have an initial value before the completion of the construction process, so it cannot be declared as a delayed attribute.

class A {
    
    
    lazy var tag = "a"
}

class B {
    
    
    lazy var a = A()
    var tag = "b"
}

let b = B()
print("b的标签是\(b.tag)")
// 打印 b的标签是b (此时a的tag属性还没有被创建)
print("b中属性a的标签是\(b.a.tag)")
// 打印 b中属性a的标签是a (此时a的tag属性已经被创建)

Calculated attributes

Calculated properties do not directly store the value, but provide a getter to obtain the value, and an optional setter to indirectly set the value of other properties or variables.

//struct Math {
    
    
//    var range = Range()
//    // middle 即为计算属性
//    var middle: Double {
    
    
//        get {
    
    
//            return (range.min + range.max) / 2.0
//        }
//        set(newCount) {
    
    
//            range.min = 0
//            range.max = newCount * 2.0
//        }
//    }
//}

// 如果计算属性的 setter 没有定义表示新值的参数名,则可以使用默认名称 newValue。

struct Math {
    
    
    var range = Range()
    // middle 即为计算属性
    var middle: Double {
    
    
        get {
    
    
            return (range.min + range.max) / 2.0
        }
        set {
    
    
            range.min = 0
            range.max = newValue * 2.0
        }
    }
}

var math = Math()
math.range.min = 1.0
math.range.max = 3.0
print("math.range = \(math.range)")
// range = Range(min: 1.0, max: 3.0)
let middle = math.middle    // 调用了get方法
print("math.middle = \(middle)")
// math.middle = 2.0

math.middle = 6             // 调用了set方法
print("math.range = \(math.range)")
// math.range = Range(min: 0.0, max: 12.0)

Read-only calculation properties

A calculated property with only a getter and no setter is called a read-only calculated property. The declaration of read-only calculated properties can remove the get keyword and curly braces:

struct MathReadOnly {
    
    
    var range = Range()
    // middle 即为计算属性
    var middle: Double {
    
    
        return (range.min + range.max) / 2.0
    }
}

var mathReadOnly = MathReadOnly()
mathReadOnly.range.min = 0.0
mathReadOnly.range.max = 2.0
print("mathReadOnly.middle = \(mathReadOnly.middle)")
// mathReadOnly.middle = 1.0

Attribute observer

The attribute observer monitors and responds to the change of the attribute value. The attribute observer is called every time the attribute is set, even when the new value is the same as the current value.
You can add one or two observers to the attribute:

  • willSet is called before the new value is set
  • didSet is called after the new value is set
class Sample {
    
    
    var count: Int = 0{
    
    
        willSet(newValue){
    
    
            print("即将设置新值: \(newValue)")
        }
        didSet{
    
    
            if count > 5 {
    
    
                print("已经设置新值, 且新值大于5: \(count)")
            }
        }
    }
}
let sample = Sample()
sample.count = 1
// 即将设置新值: 1
sample.count = 6
// 即将设置新值: 6
// 已经设置新值, 且新值大于5: 6

Type attribute

The type attribute is written in the outermost curly braces as part of the type definition, so its scope is also within the scope of the type support.
Use the keyword static to define the type attribute of the value type, and the keyword class to define the type attribute for the class.

struct AA {
    
    
    static var count = 11
}

class BB {
    
    
    class var count: Int {
    
    
        return 22
    }
}

print("AA.count = \(AA.count)")
print("BB.count = \(BB.count)")
// AA.count = 11
// BB.count = 22

AA.count = 110
print("AA.count = \(AA.count)")
AA.count = 110

Similar to instance attributes, access to type attributes is also done through the dot operator (.). However, the type attribute is obtained and set through the type itself, not through the instance.

Guess you like

Origin blog.csdn.net/u012078168/article/details/104384485