这个 session 介绍了 swift 相关的新特性。
相关文档:
distributed
Actorsmessage.ranges(of: /[a-z]at/)
这种方式时,是在编译时检查,这种方式相对更安全一些。
除此以外,swift 更是给我们提供了一种 DSL 语法让我们能够初始化 Regex,例如:Other Swift Flags
中添加“-Xfrontend -enable-bare-slash-regex”来启用这种语法。let package = Package( name: "MyStunningApp", dependencies: [ .package(url: "https://.../swift-metrics.git"), .package(url: "https://.../swift-log.git") // ⚠️ swift-log and swift-metric define a Logging module ], targets: [ .executableTarget( name: "MyStunningApp", dependencies: [ .product(name: "Logging", package: "swift-log"), .product(name: "Metrics", package: "swift-metrics", moduleAliases: ["Logging": "MetricsLogging"]), // 👈🏻 module aliasing ])])
distributed actor Player { var ai: PlayerBotAI? var gameState: GameState distributed func makeMove() -> GameMove { return ai.decideNextMove(given: &gameState) } }
if let name = name { print("Hello, \\(name)!") } if let unwrappedName = name { print("Hello, \\(unwrappedName)!") }
var name: String? = "Linda" if let name { print("Hello, \\(name)!") }
struct User { var name: String } let user: User? = User(name: "Linda") if let user.name { print("Welcome, \\(user.name)!") }
let oldResults = scores.map { score -> String in if score >= 85 { return "\\(score)%: Pass" } else { return "\\(score)%: Fail" } }
let scores = [100, 80, 85] let results = scores.map { score in if score >= 85 { return "\\(score)%: Pass" } else { return "\\(score)%: Fail" } }
let message = "the cat sat on the mat" print(message.ranges(of: "at")) print(message.replacing("cat", with: "dog")) print(message.trimmingPrefix("the "))
print(message.ranges(of: /[a-z]at/)) print(message.replacing(/[a-m]at/, with: "dog")) print(message.trimmingPrefix(/The/.ignoresCase()))
do { let atSearch = try Regex("[a-z]at") print(message.ranges(of: atSearch)) } catch { print("Failed to create regex") }
let search3 = Regex { "My name is " Capture { OneOrMore(.word) } " and I'm " Capture { OneOrMore(.digit) } " years old." }
let search4 = Regex { "My name is " Capture { OneOrMore(.word) } " and I'm " TryCapture { OneOrMore(.digit) } transform: { match in Int(match) } Capture(.digit) " years old." }
let nameRef = Reference(Substring.self) let ageRef = Reference(Int.self) let search5 = Regex { "My name is " Capture(as: nameRef) { OneOrMore(.word) } " and I'm " TryCapture(as: ageRef) { OneOrMore(.digit) } transform: { match in Int(match) } Capture(.digit) " years old." } if let result = greeting.firstMatch(of: search5) { print("Name: \\(result[nameRef])") print("Age: \\(result[ageRef])") }
func compute<C: Collection>(_ values: C = [0, 1, 2]) {}
func isSorted(array: [some Comparable]) -> Bool { array == array.sorted() }
func isSortedOld<T: Comparable>(array: [T]) -> Bool { array == array.sorted() }
func showUserDetails() -> (some Equatable, some Equatable) { (Text("Username"), Text("@twostraws")) }
func createUser() -> [some View] { let usernames = ["@frankefoster", "@mikaela__caron", "@museumshuffle"] return usernames.map(Text.init) }
func createDiceRoll() -> () -> some View { return { let diceRoll = Int.random(in: 1...6) return Text(String(diceRoll)) } }
extension Task { @available(*, deprecated, renamed: "Task.sleep(for:)") public static func sleep(_ duration: UInt64) async @available(*, deprecated, renamed: "Task.sleep(for:)") public static func sleep(nanoseconds duration: UInt64) async throws public static func sleep(for: Duration) async throws public static func sleep<C: Clock>(until deadline: C.Instant, tolerance: C.Instant.Duration? = nil, clock: C) async throws }
func compute<C: Collection>(_ values: C = [0, 1, 2]) { }