小编Bra*_*key的帖子

SwiftUI - 表单中的 NavigationLink 单元格在详细信息弹出后保持突出显示

在 iOS 14 中,似乎NavigationLinks 在Form上下文中返回后不会被取消选择。对于Form Pickers 和任何其他导致View列表中另一个的呈现(为呈现单元格提供高亮上下文)也是如此。

我在 iOS 13 中没有注意到这种行为。

一旦其他视图被关闭,有没有办法“取消选择”突出显示的行?

示例代码:

struct ContentView: View {

    var body: some View {
        Form {
            NavigationLink(destination: Text("Detail")) {
                Text("Link")
            } 
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

(不同)示例视觉:

例子

uikit ios swift swiftui

33
推荐指数
5
解决办法
4250
查看次数

SwiftUI-列表行中的多个按钮

假设我List在一行中有一个和两个按钮,如何在不突出显示整个行的情况下区分哪个按钮被轻拍了?

对于此示例代码,当点击该行中的任何一个按钮时,将同时调用两个按钮的动作回调。

// a simple list with just one row
List {

    // both buttons in a HStack so that they appear in a single row
    HStack {
        Button(action: {
            print("button 1 tapped")
        }) {
            Text("One")
        }

        Button(action: {
            print("button 2 tapped")
        }) {
            Text("Two")
        }
    }
}

// when tapping just once on either button:
// "button 1 tapped"
// "button 2 tapped"
Run Code Online (Sandbox Code Playgroud)

ios swift swiftui

14
推荐指数
4
解决办法
1747
查看次数

Cloud Firestore - 将实时侦听器更新计数作为读取操作吗?

说我正在听一个文件:

db.collection("cities").document("SF")
    .addSnapshotListener { documentSnapshot, error in
      guard let document = documentSnapshot else {
        print("Error fetching document: \(error!)")
        return
      }
      print("Current data: \(document.data())")
    }
Run Code Online (Sandbox Code Playgroud)

根据Firestore计费政策,每次更新数据都会计为读取操作,还是监听行为计数本身?

另外,我说我正在听一些文件:

db.collection("cities").whereField("state", isEqualTo: "CA")
    .addSnapshotListener { querySnapshot, error in
        guard let documents = querySnapshot?.documents else {
            print("Error fetching documents: \(error!)")
            return
        }
        let cities = documents.map { $0["name"]! }
        print("Current cities in CA: \(cities)")
    }
Run Code Online (Sandbox Code Playgroud)

我是否会立即对与查询匹配的所有文档或每个文档的每次更新 - 或两者兼而有之?

firebase google-cloud-firestore

6
推荐指数
1
解决办法
2694
查看次数

带有 async/await 的 Swift Playground“无法在范围内找到‘async’”

我正在尝试在 Xcode Playground 上运行这个异步函数:

import Foundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

enum NetworkingError: Error {
    case invalidServerResponse
    case invalidCharacterSet
}

func getJson() async throws -> String {
        let url = URL(string:"https://jsonplaceholder.typicode.com/todos/1")!
        let (data, response) = try await URLSession.shared.data(from: url)
        
        guard let httpResponse = response as? HTTPURLResponse,
              httpResponse.statusCode == 200 else {
                  throw NetworkingError.invalidServerResponse
              }
        
        guard let result = String(data: data, encoding: .utf8) else {
            throw NetworkingError.invalidCharacterSet
        }
        
        return result
}
    
let result = try! await getJson()
print(result)
Run Code Online (Sandbox Code Playgroud)

我收到此错误消息:

error: ForecastPlayground.playground:27:25: …
Run Code Online (Sandbox Code Playgroud)

async-await swift swift-playground swift5 xcode13

5
推荐指数
1
解决办法
3317
查看次数

Swift 并发 - 非阻塞 sleep()?

Swift 可以访问 Darwin 的sleep调用,使用起来很简单:

sleep(10) // waits for 10 seconds, but blocks the thread
Run Code Online (Sandbox Code Playgroud)

但是,这会阻塞当前线程。现在 Swift 已经支持async/await是否有一个内置的、非阻塞的sleep方法,不会挂起当前线程?

// what I'm looking for:

func doWork() async {
    // ...
    await sleep(10) // lightweight, does not block thread
    // ... 
}
Run Code Online (Sandbox Code Playgroud)

asynchronous async-await swift

3
推荐指数
1
解决办法
1291
查看次数