首页 > 代码库 > Swift属性
Swift属性
属性的存储
属性的主要作用是存储数据,可以常量属性和变量属 性;
[html] view plaincopy
- 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
[html] view plaincopy
- et rangeOfFourItems = FixedLengthRange(firstValue: 0, length: 4)
- // this range represents integer values 0, 1, 2, and 3 rangeOfFourItems.firstValue = 6
延时存储属性
延时存储属性是初始化时候不分配值,直到第一次使 用它。
属性@lazy 声明。
[html] view plaincopy
- 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 访问器对其访问。
[html] view plaincopy
- //定义 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 新值存储后调用。
[html] view plaincopy
- 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
定义。
[html] view plaincopy
- struct SomeStructure {
- static var storedTypeProperty = "Some value."static var computedTypeProperty: Int{
- // return anInt value here
- }
- }
- class SomeClass {
- class varcomputedTypeProperty: Int {
- // return anInt value here
- }
- }
调用的时候可以直接使用类和结构体名调用。 实例:
[html] view plaincopy
- 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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。