Swift属性
属性的存储
属性的主要作用是存储数据,可以常量属性和变量属 性;
- struct FixedLengthRange {
- var firstValue: Int let length: Int
- }
- var rangeOfThreeItems =FixedLengthRange(firstValue: 0,
- length: 3)
- // the range represents integer values 0, 1, and2 rangeOfThreeItems.firstValue = 6
- // the range now represents integer values 6, 7, and 8
但是 rangeOfFourItems 实例为常量属性也是不可以修改的。
l
- et rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
- // this range represents integer values 0, 1, 2, and 3 rangeOfFourItems.firstValue = 6
延时存储属性
延时存储属性是初始化时候不分配值,直到第一次使 用它。
属性@lazy 声明。
- class DataImporter {
- /*
- DataImporter is a class to import data from anexternalfile.
- The class is assumed to take a non-trivial amount of time toinitialize.
- */
- var fileName = "data.txt"
- // the DataImporter class would provide dataimporting functionality here
- }
- class DataManager {
- @lazy varimporter= DataImporter()
- var data = ""
- // the DataManager class would provide data management functionality here
- }
- let manager= DataManager() manager.data += "Some data" manager.data += "Some more data"
- println(manager.importer.fileName)
计算属性
有的时候一些属性是通过其他的属性计算得出的,通 过 get 和 set 访问器对其访问。
- //定义 Point struct Point {
- var x =0.0, y = 0.0
- }
- //定义 Size struct Size {
- var width = 0.0, height = 0.0
- }
- //定义 Rect 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(newCenter) {
- origin.x = newCenter.x - (size.width / 2)
- origin.y = newCenter.y - (size.height / 2)
- }
- }
- }
- var square =Rect(origin: Point(x: 0.0, y: 0.0), size: Size(width: 10.0,height: 10.0))
- let initialSquareCenter =square.center square.center = Point(x: 15.0, y: 15.0) println("square.origin is now at (\(square.origin.x),
- \(square.origin.y))")
属性观察者
为了监听属性的变化,swift 通过了属性观察者。
• willSet 观察者是在存储之前调用。
• didSet 新值存储后调用。
- class StepCounter {
- var totalSteps: Int = 0{
- willSet(newTotalSteps) {
- println("About to set totalSteps to
- \(newTotalSteps)")
- }
- didSet {
- if totalSteps >oldValue {
- steps")
- }
- println("Added \(totalSteps - oldValue)
- }
- let stepCounter = StepCounter()
- stepCounter.totalSteps = 200
- // About to set totalStepsto 200
- // Added200steps stepCounter.totalSteps = 360
- // About to set totalStepsto 360
- // Added160steps stepCounter.totalSteps = 896
- // About to set totalStepsto 896
- // Added536steps
静态属性
静态属性在结构体中使用 static 定义,类中使用 class
定义。
- struct SomeStructure {
- static var storedTypeProperty = "Some value."static var computedTypeProperty: Int{
- // return anInt value here
- }
- }
- class SomeClass {
- class varcomputedTypeProperty: Int {
- // return anInt value here
- }
- }
调用的时候可以直接使用类和结构体名调用。 实例:
- struct AudioChannel {
- static letthresholdLevel= 10
- static var maxInputLevelForAllChannels= 0 var currentLevel:Int = 0 {
- didSet {
- if currentLevel > AudioChannel.thresholdLevel {
- // cap the new audio level to the threshold level
- currentLevel = AudioChannel.thresholdLevel
- if currentLevel > AudioChannel.maxInputLevelForAllChannels {
- // storethis as the new overall maximum input level
- AudioChannel.maxInputLevelForAllChannels =
- currentLevel
- }
- }
- }
- }
- var leftChannel =AudioChannel()
- var rightChannel =AudioChannel()
- leftChannel.currentLevel = 7println(leftChannel.currentLevel)
- // prints "7"
- println(AudioChannel.maxInputLevelForAllChannels)
Swift交流讨论论坛论坛:http://www.cocoagame.net
欢迎加入Swift技术交流群:362298485