1. Stored Property

eg:

var number: Int = 0

2. Computed Property

eg:

var area : Double {
  get {
    return width * height
  }

...

}

完整代码如下:

class Rectangle {
var width: Double = 0.0
var height: Double = 0.0
var area : Double {
// computed getter
get {
return width * height
}
// computed setter
set {
// Assume equal dimensions (i.e., a square)
width = sqrt(newValue)
height = sqrt(newValue)
}
}
}

测试代码:

var rect = Rectangle()
rect.width = 3.0
rect.height = 4.5
rect.area // = 13.5
rect.area = // width & height now both 3.0

3. Property Observer(属性观察者)

class PropertyObserverExample {
var number : Int = {
willSet(newNumber) {
println("About to change to \(newNumber)")
}
didSet(oldNumber) {
println("Just changed from \(oldNumber) to \(self.number)!")
}
}
}

测试代码如下:

var observer = PropertyObserverExample()
observer.number =
// prints "About to change to 4", then "Just changed from 0 to 4!"

4. Lazy Property(属性迟绑定)

class SomeExpensiveClass {
init(id : Int) {
println("Expensive class \(id) created!")
}
} class LazyPropertyExample {
var expensiveClass1 = SomeExpensiveClass(id: )
lazy var expensiveClass2 = SomeExpensiveClass(id: )
init() {
println("First class created!")
}
}

测试代码如下:

var lazyExample = LazyPropertyExample()
// prints "Expensive class 1 created", then "First class created!"
lazyExample.expensiveClass1 // prints nothing, it's already created
lazyExample.expensiveClass2 // prints "Expensive class 2 created!"

Swift语言精要 - 属性的更多相关文章

  1. Swift语言精要 - 浅谈结构体(Struct)

    CGRect, CGSize, CGPoint这些是 . String, Int, Array, Dictionary这些我们经常用的也是结构体(Struct). 那么结构体(Struct)到底是什么 ...

  2. Swift语言精要 - Operator(运算符重载)

    运算符重载 Swift的这一语言特性或许应该启发于C++ class Vector2D { var x : Float = 0.0 var y : Float = 0.0 init (x : Floa ...

  3. Swift语言精要 - 扩展(Extension)

    swift的Extension用户在不访问代码的情况下扩展基本结构类型或自定义类 extension Int { var doubled : Int { } func multiplyWith(ano ...

  4. Swift语言精要-闭包(Closure)

    闭包(Closure)这个概念如果没学过Swift的人应该也不会陌生. 学过Javascript的朋友应该知道,在Javascript中我们经常会讨论闭包,很多前端工程师的面试题也会问到什么是闭包. ...

  5. Swift语言精要 - 序列化和反序列化

    在swift中你可以把一个对象转换成为数据,你所要做的就是 首先,你需要让对象实现NSObject和NSCoding协议. 其次,实现以下两个方法: encodeWithCoder init(code ...

  6. Swift语言精要 - 浅谈代理模式(Delegate)

    在iOS编程中,我们经常谈到代理代理,也就是delegate,那么什么是代理呢? 我们来看一下cocoa对它的描述: Delegation is Cocoa’s term for passing of ...

  7. Swift语言精要 - Dictionary(字典)

    字典以键值对的形式存储数据. 键不能重复,但是值可以重复. 基本语法用例: var states : Dictionary<String, String> = ["CA" ...

  8. Swift语言快速入门

    Swift语言快速入门(首部同步新版官方API文档和语法的Swift图书,确保代码可编译,作者专家在线答疑,图书勘误实时跟进) 极客学院 编著   ISBN 978-7-121-24328-8 201 ...

  9. 赶时髦过了一遍Swift 语言....

    Swift 语言 2014年6月3日发布,替代OBJECT-C Swift is a new programming language for creating iOS and OS X apps. ...

随机推荐

  1. dev的documentManager,多个tab窗体

    private void AddDocument(Funcation CurrentModel) { if (!string.IsNullOrWhiteSpace(CurrentModel.Funct ...

  2. 【转】IntelliJ IDEA关联SVN

    http://blog.csdn.net/xdd19910505/article/details/52756417 问题描述: IntelliJ IDEA安装之后,使用SVN进行提交或更新以及检出代码 ...

  3. C语言之基本算法24—黄金切割法求方程近似根

    //黄金切割法! /* ================================================================ 题目:用黄金切割法求解3*x*x*x-2*x* ...

  4. python测试开发django-16.JsonResponse返回中文编码问题

    前言 django查询到的结果,用JsonResponse返回在页面上显示类似于\u4e2d\u6587 ,注意这个不叫乱码,这个是unicode编码,python3默认返回的编码 遇到问题 接着前面 ...

  5. Linux学习16-CentOS安装gitlab环境

    前言 在学习Gitlab的环境搭建之前,首先需要了解Git,Gitlab,GitHub他们三者之间的关系 Git 它是一个源代码版本控制系统,可让您在本地跟踪更改并从远程资源推送或提取更改. GitH ...

  6. 多个Jar的合并操作

    同事要写Android平台下的打包工具,遇到需要将多个jar合并成一个jar的问题.这里列一下操作步骤: 1.将所有jar文件复制至某临时目录中,通过jar命令解压得到所有的.class文件 > ...

  7. GridView和SimpleAdapter实现网格布局

    android:horizontalSpacing 元素之间的水平间距 android:verticalSpacing     元素之间的垂直间距 android:numColumns         ...

  8. Protobuf 协议语言指南

    l  定义一个消息(message)类型 l  标量值类型 l  Optional 的字段及默认值 l  枚举 l  使用其他消息类型 l  嵌套类型 l  更新一个消息类型 l  扩展 l  包(p ...

  9. [转]PHP: 深入pack/unpack

    From : http://my.oschina.net/goal/blog/195749 http://www.w3school.com.cn/php/func_misc_pack.asp PHP作 ...

  10. [转]有关Apache alias的一点问题

    转自:http://www.thinkphp.cn/topic/11973.html Apache 的Alias 指令映射URL到文件系统的特定区域 一个简单的例子: Alias /mytest /w ...