📅  最后修改于: 2021-01-11 07:56:18             🧑  作者: Mango
Swift4属性用于关联类,枚举或结构的值。
Swift4属性可以分为两种类型:
Stored Property | Computed Property |
---|---|
The stored property stores constant and variable values as instance. | The computed property is used to calculate a value rather than storing the value. |
The stored property is provided by classes and structures. | The computed property is provided by classes, enumerations and structures |
存储属性和计算属性都与实例类型相关联。当属性与其类型值相关联时,则将其定义为“类型属性”。
存储和计算的属性通常与特定类型的实例相关联。属性也可以与类型本身关联。这样的属性称为类型属性。
属性观察器用于观察存储属性的值,也可以观察从超类派生的继承子类的属性。
在Swift4中,常量的存储属性由'let'关键字定义,变量的存储属性由'var'关键字定义。存储的属性在定义期间提供“默认值”,并且用户可以在初始化期间对其进行初始化和修改。
struct Number {
var digits: Int
let pi = 3.1415
}
var n = Number(digits: 123456789)
n.digits = 910
print("\(n.digits)")
print("\(n.pi)")
输出:
910
3.1415
在此,实例pi = 3.1415将变量pi初始化为存储的属性值。因此,无论何时引用该实例,它都将保留值3.1415。
惰性存储属性是Swift4的灵活属性。首次初始化变量时,它不计算初始值。变量声明之前使用“惰性”修饰符作为惰性存储属性。
class sample {
lazy var no = number() // `var` declaration is required.
}
class number {
var name = "JavaTpoint"
}
var firstsample = sample()
print(firstsample.no.name)
输出:
JavaTpoint
在Objective-C中,存储的属性具有用于备份的实例变量,并存储在存储的属性中声明的值。
Swift 4将这两个概念合并到单个“存储属性”声明中。 “存储的属性”不包含相应的实例变量和备份值,而是包含通过变量名称,数据类型和内存管理功能在单个位置定义的关于变量属性的所有集成信息。
计算属性提供了一个getter和一个可选的setter,以间接检索和设置其他属性和值,而不是存储它们。
class sample {
var no1 = 0.0, no2 = 0.0
var length = 200.0, breadth = 100.0
var middle: (Double, Double) {
get {
return (length / 2, breadth / 2)
}
set(axis){
no1 = axis.0 - (length / 2)
no2 = axis.1 - (breadth / 2)
}
}
}
var result = sample()
print(result.middle)
result.middle = (0.0, 10.0)
print(result.no1)
print(result.no2)
输出:
(100.0, 50.0)
-100.0
-40.0
Swift4使用属性观察器来观察和响应属性值。设置属性值时,每次都会调用属性观察器。除了惰性存储的属性外,我们还可以通过“覆盖”方法将属性观察器添加到“继承的”属性中。
您可以定义属性观察者之一:
class Samplepgm {
var counter: Int = 0 {
willSet(newTotal){
print("Total Counter is: \(newTotal)")
}
didSet {
if counter > oldValue {
print("Newly Added Counter \(counter - oldValue)")
}
}
}
}
let NewCounter = Samplepgm()
NewCounter.counter = 100
NewCounter.counter = 500
输出:
Total Counter is: 100
Newly Added Counter 100
Total Counter is: 500
Newly Added Counter 400
局部变量和全局变量都用于计算和观察属性。
Local Variables | Global Variables |
---|---|
Local Variables are defined within a function, method, or closure context. | Global Variables are defined outside function, method, closure, or type context. |
Local Variables are used to store and retrieve values. | Global variables are used to store and retrieve values. |
Stored properties are used to get and set the values. | Stored properties are used to get and set the values. |
In local variables, computed properties are used. | In global variables, computed properties are also used. |
Swift4属性在大括号{}的类型定义部分中定义,并且变量的范围也已在前面定义。要定义值类型的类型属性,“静态”?使用关键字,对于类类型,使用?class?使用关键字。
struct Struct_Name {
static var storedTypeProperty = " "
static var computedTypeProperty: Int {
// return an Int value here
}
}
enum Enum_Name {
static var storedTypeProperty = " "
static var computedTypeProperty: Int {
// return an Int value here
}
}
class Class_Name {
class var computedTypeProperty: Int {
// return an Int value here
}
}
Swift4类型属性的设置和查询与实例属性相同。它用 '。'语法仅针对类型,而不是指向实例。
struct StudMarks {
static let markCount = 85
static var totalCount = 0
var InternalMarks: Int = 0 {
didSet {
if InternalMarks > StudMarks.markCount {
InternalMarks = StudMarks.markCount
}
if InternalMarks > StudMarks.totalCount {
StudMarks.totalCount = InternalMarks
}
}
}
}
var stud1Mark1 = StudMarks()
var stud1Mark2 = StudMarks()
stud1Mark1.InternalMarks = 80
print(stud1Mark1.InternalMarks)
stud1Mark2.InternalMarks = 90
print(stud1Mark2.InternalMarks)
输出:
80
85