Swift属性

属性的存储

属性的主要作用是存储数据,可以常量属性和变量属 性;

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. struct FixedLengthRange {  
  2. var firstValue: Int let length: Int  
  3. }  
  4. var rangeOfThreeItems =FixedLengthRange(firstValue: 0,  
  5. length: 3)   
  6. // the range represents integer values 0, 1, and2 rangeOfThreeItems.firstValue = 6  
  7. // the range now represents integer values 6, 7, and 8  

但是 rangeOfFourItems 实例为常量属性也是不可以修改的。

l

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. et rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)  
  2. // this range represents integer values 0, 1, 2, and 3 rangeOfFourItems.firstValue = 6  

延时存储属性

延时存储属性是初始化时候不分配值,直到第一次使 用它。

属性@lazy 声明。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. class DataImporter {  
  2. /*  
  3. DataImporter is a class to import data from anexternalfile.  
  4. The   class  is assumed     to  take  a  non-trivial amount of time toinitialize.  
  5. */  
  6. var fileName = "data.txt"  
  7. // the  DataImporter   class  would   provide  dataimporting functionality here  
  8. }  
  9. class DataManager {  
  10. @lazy varimporterDataImporter()  
  11. var data = ""  
  12. // the DataManager class would provide data management functionality here  
  13. }  
  14. let managerDataManager() manager.data += "Some data" manager.data += "Some more data"  
  15. println(manager.importer.fileName)  

计算属性

有的时候一些属性是通过其他的属性计算得出的,通 过 get 和 set 访问器对其访问。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. //定义 Point struct Point {  
  2. var x =0.0, y = 0.0  
  3. }  
  4. //定义 Size struct Size {  
  5. var width = 0.0, height = 0.0  
  6. }  
  7. //定义 Rect struct Rect {  
  8. var origin = Point()  
  9. var size = Size()  
  10. var center: Point {  
  11. get {  
  12. let centerX = origin.x+ (size.width / 2)  
  13. let centerY = origin.y + (size.height / 2)  
  14. return Point(x: centerX, y: centerY)  
  15. }  
  16. set(newCenter) {  
  17. origin.x = newCenter.x - (size.width / 2)  
  18. origin.y = newCenter.y - (size.height / 2)   
  19. }  
  20. }  
  21. }  
  22. var square =Rect(origin: Point(x: 0.0, y: 0.0), size: Size(width: 10.0,height: 10.0))  
  23. let initialSquareCenter =square.center square.center = Point(x: 15.0, y: 15.0) println("square.origin is  now    at  (\(square.origin.x),  
  24. \(square.origin.y))")  
  25.    

属性观察者

为了监听属性的变化,swift 通过了属性观察者。

• willSet 观察者是在存储之前调用。

• didSet 新值存储后调用。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. class StepCounter {  
  2. var totalSteps: Int = 0{  
  3. willSet(newTotalSteps) {  
  4. println("About    to    set    totalSteps    to  
  5. \(newTotalSteps)")  
  6. }  
  7. didSet {  
  8. if totalSteps >oldValue   {  
  9. steps")  
  10. }  
  11. println("Added   \(totalSteps  - oldValue)  
  12. }  
  13. let stepCounter = StepCounter()  
  14. stepCounter.totalSteps = 200  
  15. // About to set totalStepsto 200  
  16. // Added200steps stepCounter.totalSteps = 360  
  17. // About to set totalStepsto 360  
  18. // Added160steps stepCounter.totalSteps = 896  
  19. // About to set totalStepsto 896  
  20. // Added536steps  

静态属性

静态属性在结构体中使用 static 定义,类中使用 class

定义。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. struct SomeStructure {  
  2. static var storedTypeProperty = "Some value."static var computedTypeProperty: Int{  
  3. // return anInt value here   
  4. }  
  5. }  
  6. class SomeClass {  
  7. class varcomputedTypeProperty: Int {  
  8. // return anInt value here  
  9. }  
  10. }  

调用的时候可以直接使用类和结构体名调用。 实例:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. struct AudioChannel {  
  2. static letthresholdLevel10  
  3. static var maxInputLevelForAllChannels0 var currentLevel:Int = 0 {  
  4. didSet {  
  5. if               currentLevel                 > AudioChannel.thresholdLevel {  
  6. // cap   the  new   audio   level  to  the threshold level  
  7. currentLevel                             = AudioChannel.thresholdLevel  
  8. if               currentLevel                 > AudioChannel.maxInputLevelForAllChannels {  
  9. // storethis as the new overall maximum input level  
  10. AudioChannel.maxInputLevelForAllChannels          =  
  11. currentLevel  
  12. }  
  13. }  
  14. }  
  15. }  
  16. var leftChannel =AudioChannel()  
  17. var rightChannel =AudioChannel()  
  18. leftChannel.currentLevel = 7println(leftChannel.currentLevel)  
  19. // prints "7"  
  20. println(AudioChannel.maxInputLevelForAllChannels)  

 

Swift交流讨论论坛论坛:http://www.cocoagame.net

欢迎加入Swift技术交流群:362298485

猜你喜欢

转载自guandongsheng.iteye.com/blog/2086583