闭包属性
This commit is contained in:
@@ -142,3 +142,11 @@ var p = Point() class_getInstanceSize(type(of: p)) // 40
|
|||||||
class_getInstanceSize(Point.self) // 40
|
class_getInstanceSize(Point.self) // 40
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### 对象中的方法
|
||||||
|
|
||||||
|
方法占用对象的内存吗?
|
||||||
|
|
||||||
|
* 不占用
|
||||||
|
* 方法的本质是函数
|
||||||
|
* 方法以及函数都放在代码段
|
||||||
|
|
||||||
|
121
09 闭包.playground/Contents.swift
Normal file
121
09 闭包.playground/Contents.swift
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
import UIKit
|
||||||
|
|
||||||
|
var greeting = "Hello, playground"
|
||||||
|
|
||||||
|
/*:
|
||||||
|
swift 中可以通过 func 定义一个函数,也可以通过闭包表达式创建一个函数。
|
||||||
|
|
||||||
|
闭包表达式(Closure Expression)
|
||||||
|
- 闭包表达式的格式
|
||||||
|
{
|
||||||
|
(参数列表) -> 返回值类型 in
|
||||||
|
函数体代码
|
||||||
|
}
|
||||||
|
|
||||||
|
- 闭包表达式的简写
|
||||||
|
|
||||||
|
尾随闭包: 尾随闭包是一个被书写在函数调用括号外面(后面)的闭包表达式
|
||||||
|
- 如果将一个很长的闭包表达式作为函数的最后一个实参,使用尾随闭包可以增强函数的可读性
|
||||||
|
- 如果闭包表达式是函数的唯一实参,而且使用了尾随闭包的语法,那就不需要在函数名后边写圆括号
|
||||||
|
|
||||||
|
|
||||||
|
闭包(Closure)
|
||||||
|
- 一个函数和它所捕获的变量\常量环境组合起来,称为闭包
|
||||||
|
- 一般指定义在函数内部的函数
|
||||||
|
- 一般它捕获的是外层函数的局部变量\常量
|
||||||
|
|
||||||
|
- 可以把闭包想象成是一个类的实例对象
|
||||||
|
- 内存在堆空间
|
||||||
|
- 捕获的局部变量\常量就是对象的成员(存储属性)
|
||||||
|
- 组成闭包的函数就是类内部定义的方法
|
||||||
|
|
||||||
|
class Closure {
|
||||||
|
var num = 0
|
||||||
|
func plus(_ i: Int) -> Int { num += i
|
||||||
|
return num
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var cs1 = Closure()
|
||||||
|
var cs2 = Closure()
|
||||||
|
cs1.plus(1) // 1
|
||||||
|
cs2.plus(2) // 2
|
||||||
|
cs1.plus(3) // 4
|
||||||
|
cs2.plus(4) // 6
|
||||||
|
cs1.plus(5) // 9
|
||||||
|
cs2.plus(6) // 12
|
||||||
|
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 闭包表达式
|
||||||
|
var fn = {
|
||||||
|
(v1: Int, v2: Int) -> Int in
|
||||||
|
return v1 + v2
|
||||||
|
}
|
||||||
|
|
||||||
|
print(fn(10, 20))
|
||||||
|
|
||||||
|
// 尾随闭包
|
||||||
|
// 闭包表达式作为函数的最后一个实参
|
||||||
|
func exec(v1: Int, v2: Int, fn:(Int, Int) -> Int) {
|
||||||
|
print(fn(v1, v2))
|
||||||
|
}
|
||||||
|
|
||||||
|
exec(v1: 10, v2: 20) {
|
||||||
|
$0 + $1
|
||||||
|
}
|
||||||
|
|
||||||
|
// 闭包表达式是函数的唯一实参
|
||||||
|
func exec1(fn:(Int, Int) -> Int) {
|
||||||
|
print(fn(1, 2))
|
||||||
|
}
|
||||||
|
|
||||||
|
exec1(fn: {$0 + $1})
|
||||||
|
exec1() {$0 + $1}
|
||||||
|
exec1{$0 + $1}
|
||||||
|
|
||||||
|
// 闭包
|
||||||
|
typealias Fn = (Int) -> Int
|
||||||
|
func getFn() -> Fn {
|
||||||
|
var num = 0
|
||||||
|
func plus(_ i: Int) -> Int { num += i
|
||||||
|
return num
|
||||||
|
}
|
||||||
|
return plus
|
||||||
|
} // 返回的plus和num形成了闭包
|
||||||
|
|
||||||
|
var fn1 = getFn() // 分配堆空间
|
||||||
|
var fn2 = getFn() // 分配另外的堆空间
|
||||||
|
fn1(1) // 1
|
||||||
|
fn2(2) // 2
|
||||||
|
fn1(3) // 4
|
||||||
|
fn2(4) // 6
|
||||||
|
fn1(5) // 9
|
||||||
|
fn2(6) // 12
|
||||||
|
|
||||||
|
// 自动闭包
|
||||||
|
/*:
|
||||||
|
@autoclosure 会自动将 20 封装成闭包 { 20 }
|
||||||
|
@autoclosure 只支持 () -> T 格式的参数
|
||||||
|
@autoclosure 并非只支持最后1个参数
|
||||||
|
空合并运算符 ?? 使用了 @autoclosure 技术
|
||||||
|
有@autoclosure、无@autoclosure,构成了函数重载
|
||||||
|
*/
|
||||||
|
// 如果第1个数大于0,返回第一个数。否则返回第2个数
|
||||||
|
func getFirstPositive(_ v1: Int, _ v2: Int) -> Int {
|
||||||
|
return v1 > 0 ? v1 : v2
|
||||||
|
}
|
||||||
|
getFirstPositive(10, 20) // 10
|
||||||
|
getFirstPositive(-2, 20) // 20
|
||||||
|
getFirstPositive(0, -4) // -4
|
||||||
|
|
||||||
|
// 改成函数类型的参数,可以让v2延迟加载
|
||||||
|
func getFirstPositive(_ v1: Int, _ v2: () -> Int) -> Int? {
|
||||||
|
return v1 > 0 ? v1 : v2()
|
||||||
|
}
|
||||||
|
getFirstPositive(-4) { 20 }
|
||||||
|
|
||||||
|
func getFirstPositive(_ v1: Int, _ v2: @autoclosure () -> Int) -> Int? { return v1 > 0 ? v1 : v2()
|
||||||
|
}
|
||||||
|
getFirstPositive(-4, 20)
|
||||||
|
|
4
09 闭包.playground/contents.xcplayground
Normal file
4
09 闭包.playground/contents.xcplayground
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||||
|
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
|
||||||
|
<timeline fileName='timeline.xctimeline'/>
|
||||||
|
</playground>
|
7
09 闭包.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
7
09 闭包.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Workspace
|
||||||
|
version = "1.0">
|
||||||
|
<FileRef
|
||||||
|
location = "group:">
|
||||||
|
</FileRef>
|
||||||
|
</Workspace>
|
BIN
09 闭包.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate
generated
Normal file
BIN
09 闭包.playground/playground.xcworkspace/xcuserdata/jiulinxiri.xcuserdatad/UserInterfaceState.xcuserstate
generated
Normal file
Binary file not shown.
20
10 属性 inout.playground/Contents.swift
Normal file
20
10 属性 inout.playground/Contents.swift
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
import UIKit
|
||||||
|
|
||||||
|
|
||||||
|
struct Circle {
|
||||||
|
// 存储属性
|
||||||
|
var radius: Double
|
||||||
|
// 计算属性
|
||||||
|
var diameter: Double {
|
||||||
|
set {
|
||||||
|
radius = newValue / 2
|
||||||
|
}
|
||||||
|
get {
|
||||||
|
radius * 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var circle = Circle(radius: 5)
|
||||||
|
print(circle.radius)
|
||||||
|
print(circle.diameter)
|
32
10 属性 inout.playground/Resources/README.md
Normal file
32
10 属性 inout.playground/Resources/README.md
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
### 属性
|
||||||
|
|
||||||
|
Swift 中实例相关的属性分为两大类
|
||||||
|
|
||||||
|
* 存储属性
|
||||||
|
* 计算属性
|
||||||
|
|
||||||
|
```swift
|
||||||
|
struct Circle {
|
||||||
|
// 存储属性
|
||||||
|
var radius: Double
|
||||||
|
// 计算属性
|
||||||
|
var diameter: Double {
|
||||||
|
set {
|
||||||
|
radius = newValue / 2
|
||||||
|
}
|
||||||
|
get {
|
||||||
|
radius * 2
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#### 存储属性(Stored Property)
|
||||||
|
|
||||||
|
1. 类似于成员变量
|
||||||
|
2. 存储在实例的内存中
|
||||||
|
3. 结构体、类可以定义存储属性
|
||||||
|
4. 枚举 `不可以` 定义存储属性
|
||||||
|
|
4
10 属性 inout.playground/contents.xcplayground
Normal file
4
10 属性 inout.playground/contents.xcplayground
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||||
|
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
|
||||||
|
<timeline fileName='timeline.xctimeline'/>
|
||||||
|
</playground>
|
7
10 属性 inout.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
7
10 属性 inout.playground/playground.xcworkspace/contents.xcworkspacedata
generated
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<Workspace
|
||||||
|
version = "1.0">
|
||||||
|
<FileRef
|
||||||
|
location = "group:">
|
||||||
|
</FileRef>
|
||||||
|
</Workspace>
|
Binary file not shown.
Reference in New Issue
Block a user