戴铭

Results 93 issues of 戴铭

@dynamicMemberLookup 指示访问属性时调用一个已实现的处理动态查找的下标方法 subscript(dynamicMemeber:),通过指定属性字符串名返回值。使用方法如下: ```swift @dynamicMemberLookup struct D { // 找字符串 subscript(dynamicMember m: String) -> String { let p = ["one": "first", "two": "second"] return p[m, default: ""] } // 找整型...

```swift #if targetEnvironment(simulator) // 模拟器 #else // 真机 #endif ```

```swift #if canImport(SpriteKit) // iOS 等苹果系统执行 #else // 非苹果系统 #endif ```

数字的类型有 Int、Float 和 Double ```swift // Int let i1 = 100 let i2 = 22 print(i1 / i2) // 向下取整得 4 // Float let f1: Float = 100.0 let f2:...

所有相关提案清单如下: * [SE-0296: Async/await](https://github.com/apple/swift-evolution/blob/main/proposals/0296-async-await.md) [【译】SE-0296 Async/await](https://kemchenj.github.io/2021-03-06/) * [SE-0317: async let](https://github.com/apple/swift-evolution/blob/main/proposals/0317-async-let.md) * [SE-0300: Continuations for interfacing async tasks with synchronous code](https://github.com/apple/swift-evolution/blob/main/proposals/0300-continuation.md) [【译】SE-0300 Continuation – 执行同步代码的异步任务接口](https://kemchenj.github.io/2021-03-31/) * [SE-0302: Sendable and @Sendable closures](https://github.com/apple/swift-evolution/blob/main/proposals/0302-concurrent-value-and-concurrent-closures.md)...

参考: * [Swift Style Guide](https://google.github.io/swift/) 多用静态特性。swift 在编译期间所做的优化比 OC 要多,这是由于他的静态派发、泛型特化、写时复制这些静态特性决定的。另外通过 final 和 private 这样的表示可将动态特性转化为静态方式,编译开启 WMO 可以自动推导出哪些动态派发可转化为静态派发。 如何避免崩溃? * 字典:用结构体替代 * Any:可用泛型或关联关联类型替代 * as? :少用 AnyObject,多用泛型或不透明类型 * !:要少用 好的实践? * 少用继承,多用 protocol...

## 动态 欢迎你的内容分享和留言,请发到 [议题里](https://github.com/ming1016/SwiftPamphletApp/issues),也欢迎你的 PR :) 。 * 22.01.03 发布2.0:新动态支持提醒,包括博客RSS、开发者、好库和探索库 * 21.11.16 发布1.0:Swift 指南语法速查完成 ## 资源 * 官方:[apple/swift](https://github.com/apple/swift)、[Swift.org](https://www.swift.org/)、[Swift.org - Blog](https://www.swift.org/blog/)、[The Swift Programming Language](https://docs.swift.org/swift-book/) [中文](https://github.com/SwiftGGTeam/the-swift-programming-language-in-chinese)、[WWDC Session](https://developer.apple.com/videos/)、[SwiftUI 官方例子](https://developer.apple.com/tutorials/sample-apps) * 手册:[SwiftDoc.org](https://swiftdoc.org/)、[Swift 5.1 Cheat...

```swift let s1 = "Hi! This is a string. Cool?" /// 转义符 \n 表示换行。 /// 其它转义字符有 \0 空字符)、\t 水平制表符 、\n 换行符、\r 回车符 let s2 = "Hi!\nThis is a string. Cool?"...

字典是无序集合,键值对应。 ```swift var d1 = [ "k1": "v1", "k2": "v2" ] d1["k3"] = "v3" d1["k4"] = nil print(d1) // ["k2": "v2", "k3": "v3", "k1": "v1"] for (k, v) in d1...

泛型可以减少重复代码,是一种抽象的表达方式。where 关键字可以对泛型做约束。 ```swift func fn(p: T) -> [T] { var r = [T]() r.append(p) return r } print(fn(p: "one")) // 结构体 struct S1 { var arr = [T]() mutating func...