-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
879 additions
and
128 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
43 changes: 43 additions & 0 deletions
43
SwiftPamphletApp/Resource/Guide/SwiftUI/图文组件/Text/Text-动态时间(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
|
||
逻辑更新时,不会触发 `body` 的重绘,只对使用了 Text Date 插值的 `Text`。 | ||
|
||
倒计时 | ||
|
||
```swift | ||
Text(Date().addingTimeInterval(60), style: .offset) | ||
``` | ||
|
||
指定多长时间 | ||
|
||
```swift | ||
// 多久后 | ||
let aimDate = Calendar.current.date(byAdding: DateComponents(minute: 10), to: Date())! | ||
|
||
``` | ||
|
||
指定一个具体时间 | ||
|
||
```swift | ||
// 具体时间 | ||
let aimDate = Calendar.current(DateComponents(year: 2024, month: 6, day: 11, hour: 20, minute: 10))! | ||
``` | ||
|
||
Text 视图 | ||
|
||
```swift | ||
// 多种表现样式 | ||
Text(aimDate, style: .relative) | ||
Text(aimDate, style: .offset) | ||
Text(aimDate, style: .timer) | ||
Text(aimDate, style: .date) | ||
Text(aimDate, style: .time) | ||
``` | ||
|
||
两个时间间隔 | ||
|
||
```swift | ||
let beginDate = Calendar.current.date(from: DateComponents(year: 2024, month: 6, day: 11, hour: 20, minute: 10))! | ||
let endDate = Calendar.current.date(from: DateComponents(year: 2024, month: 6, day: 15, hour: 20, minute: 10))! | ||
|
||
Text(beginDate ... endDate) | ||
``` |
78 changes: 20 additions & 58 deletions
78
SwiftPamphletApp/Resource/Guide/基础库/自带属性包装/@dynamicCallable动态可调用类型(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,28 @@ | ||
结果生成器(Result builders),通过传递序列创建新值,SwiftUI就是使用的结果生成器将多个视图生成一个视图 | ||
|
||
@dynamicCallable 动态可调用类型。通过实现 dynamicallyCall 方法来定义变参的处理。 | ||
|
||
```swift | ||
@resultBuilder | ||
struct RBS { | ||
// 基本闭包支持 | ||
static func buildBlock(_ components: Int...) -> Int { | ||
components.reduce(0) { partialResult, i in | ||
partialResult + i | ||
} | ||
} | ||
// 支持条件判断 | ||
static func buildEither(first component: Int) -> Int { | ||
component | ||
} | ||
static func buildEither(second component: Int) -> Int { | ||
component | ||
@dynamicCallable | ||
struct D { | ||
// 带参数说明 | ||
func dynamicallyCall(withKeywordArguments args: KeyValuePairs<String, Int>) -> Int { | ||
let firstArg = args.first?.value ?? 0 | ||
return firstArg * 2 | ||
} | ||
// 支持循环 | ||
static func buildArray(_ components: [Int]) -> Int { | ||
components.reduce(0) { partialResult, i in | ||
partialResult + i | ||
|
||
// 无参数说明 | ||
func dynamicallyCall(withArguments args: [String]) -> String { | ||
var firstArg = "" | ||
if args.count > 0 { | ||
firstArg = args[0] | ||
} | ||
return "show \(firstArg)" | ||
} | ||
} | ||
|
||
let a = RBS.buildBlock( | ||
1, | ||
2, | ||
3 | ||
) | ||
print(a) // 6 | ||
|
||
// 应用到函数中 | ||
@RBS func f1() -> Int { | ||
1 | ||
2 | ||
3 | ||
} | ||
print(f1()) // 6 | ||
|
||
// 设置了 buildEither 就可以在闭包中进行条件判断。 | ||
@RBS func f2(stopAtThree: Bool) -> Int { | ||
1 | ||
2 | ||
3 | ||
if stopAtThree == true { | ||
0 | ||
} else { | ||
4 | ||
5 | ||
6 | ||
} | ||
} | ||
print(f2(stopAtThree: false)) // 21 | ||
|
||
// 设置了 buildArray 就可以在闭包内使用循环了 | ||
@RBS func f3() -> Int { | ||
for i in 1...3 { | ||
i * 2 | ||
} | ||
} | ||
print(f3()) // 12 | ||
let d = D() | ||
let i = d(numberIs: 2) | ||
print(i) // 4 | ||
let s = d("hi") | ||
print(s) // show hi | ||
``` | ||
|
||
[SE-0348 buildPartialBlock for result builders](https://github.com/apple/swift-evolution/blob/main/proposals/0348-buildpartialblock.md) 简化了实现复杂 result buiders 所需的重载。 |
52 changes: 35 additions & 17 deletions
52
SwiftPamphletApp/Resource/Guide/基础库/自带属性包装/@dynamicMemberLookup动态成员查询(ap).md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,45 @@ | ||
@dynamicCallable 动态可调用类型。通过实现 dynamicallyCall 方法来定义变参的处理。 | ||
|
||
@dynamicMemberLookup 指示访问属性时调用一个已实现的处理动态查找的下标方法 subscript(dynamicMemeber:),通过指定属性字符串名返回值。使用方法如下: | ||
|
||
```swift | ||
@dynamicCallable | ||
@dynamicMemberLookup | ||
struct D { | ||
// 带参数说明 | ||
func dynamicallyCall(withKeywordArguments args: KeyValuePairs<String, Int>) -> Int { | ||
let firstArg = args.first?.value ?? 0 | ||
return firstArg * 2 | ||
// 找字符串 | ||
subscript(dynamicMember m: String) -> String { | ||
let p = ["one": "first", "two": "second"] | ||
return p[m, default: ""] | ||
} | ||
// 找整型 | ||
subscript(dynamicMember m: String) -> Int { | ||
let p = ["one": 1, "two": 2] | ||
return p[m, default: 0] | ||
} | ||
|
||
// 无参数说明 | ||
func dynamicallyCall(withArguments args: [String]) -> String { | ||
var firstArg = "" | ||
if args.count > 0 { | ||
firstArg = args[0] | ||
// 找闭包 | ||
subscript(dynamicMember m: String) -> (_ s: String) -> Void { | ||
return { | ||
print("show \($0)") | ||
} | ||
return "show \(firstArg)" | ||
} | ||
// 静态数组成员 | ||
var p = ["This is a member"] | ||
// 动态数组成员 | ||
subscript(dynamicMember m: String) -> [String] { | ||
return ["This is a dynamic member"] | ||
} | ||
} | ||
|
||
let d = D() | ||
let i = d(numberIs: 2) | ||
print(i) // 4 | ||
let s = d("hi") | ||
print(s) // show hi | ||
let s1: String = d.one | ||
print(s1) // first | ||
let i1: Int = d.one | ||
print(i1) // 1 | ||
d.show("something") // show something | ||
print(d.p) // ["This is a member"] | ||
let dynamicP:[String] = d.dp | ||
print(dynamicP) // ["This is a dynamic member"] | ||
``` | ||
|
||
类使用 @dynamicMemberLookup,继承的类也会自动加上 @dynamicMemberLookup。协议上定义 @dynamicMemberLookup,通过扩展可以默认实现 subscript(dynamicMember:) 方法。 | ||
|
||
|
||
|
Oops, something went wrong.