屬性的存儲
屬性的主要作用是存儲數據,可以常量屬性和變量屬 性;
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