闭包属性

This commit is contained in:
2022-01-07 22:16:23 +08:00
parent dd9b336de0
commit 5934fb2aea
10 changed files with 203 additions and 0 deletions

View File

@@ -142,3 +142,11 @@ var p = Point() class_getInstanceSize(type(of: p)) // 40
class_getInstanceSize(Point.self) // 40 class_getInstanceSize(Point.self) // 40
``` ```
#### 对象中的方法
方法占用对象的内存吗?
* 不占用
* 方法的本质是函数
* 方法以及函数都放在代码段

View 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
} // plusnum
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
*/
// 102
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)

View 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>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:">
</FileRef>
</Workspace>

View 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)

View 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. 枚举 `不可以` 定义存储属性

View 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>

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "group:">
</FileRef>
</Workspace>