# 存储属性

/**
*  存储属性
*/
struct MyRange {
var firstValue: Int
let length: Int
}

var range = MyRange(firstValue: 0, length: 3)

range.firstValue = 3;

///结构体常量不能改变里面的存储属性
let constMyRange = MyRange(firstValue: 5, length: 6)



# 懒加载属性

//在属性前面添加一个lazy

lazy vay count: Int

# 计算属性

/**
*  计算属性
*/

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
let centerY = origin.y + size.height / 2
return Point(x: centerX, y: centerY)
}
set(newValue) {
origin.x = newValue.x - size.width / 2
origin.y = newValue.y - size.height / 2
}
}
}

var square = Rect(origin: Point(x: 0.0, y: 0.0), size: Size(width: 10.0, height: 10.0))

let squareCenter = square.center

square.center = Point(x: 15.0, y: 15.0)

# 属性监测器

willSet和didSet方法

class StepCount {
var total:Int = 0 {
willSet {
print("willSet run")
}
didSet {
print("didSet run")
}
}
}

var stepCounter = StepCount()

///开始会执行willSet方法，赋值完以后会执行didSet方法
stepCounter.total = 200

# 类型属性

/**
*  类型属性
*/

struct myStruct {
static var myString = "hello"
static var intProperty: Int {
return 5
}
}

enum myEnum {
static var myString = "hello"
static var intProperty: Int {
return 5
}
}

class myClass {
//类中不支持存储属性位类属性
//    class var myString = "hello"
class var intProperty: Int {
return 5
}
}


• 本文已收录于以下专栏：

举报原因： 您举报文章：【swift-总结】属性 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)