如果我们有一个简单的协议和类实现,如下所示;
protocol Solution: ObservableObject {
var result: String { get set }
func calc() async
}
@MainActor
class Solve: Solution {
@Published
var result: String = ""
func calc() async { // operate on actors to find the result
result = "the answer"
}
}
Run Code Online (Sandbox Code Playgroud)
Xcode 将显示黄色警告:“Main actor-isolated property 'result'不能用于满足非隔离协议要求”针对结果的类定义。
删除 @MainActor 将删除警告,但随后我们需要手动Dispatch
更新结果以确保它们在主线程上完成。有没有更干净的方法来做到这一点?也许通过修改协议?
从 Xcode 12.3 开始,当您使用 macOS 目标创建新的 SwiftUI 项目时,Xcode 默认目标为 11.0 (Big Sur)。不更改默认创建的默认“hello world”应用程序中的一行代码,然后我将目标更改为 macOS 10.15(这是我仍在使用的)默认项目 swift 文件将不再构建。:-
@main
struct catalinaApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}```
3 Errors: @main, 'Scene' and WindowGroup is only available in macOS 11.0 or newer
How can I alter the project/above file to correctly build the default app on Catalina? (10.15) ?
thanks in advance!
Run Code Online (Sandbox Code Playgroud)