如何在 Xcode 15 Beta 中使用 @Observable?

huangapple go评论60阅读模式
英文:

How to use @Observable in Xcode 15 Beta?

问题

我刚刚在我的运行首个 macOS Sonoma beta 版的开发机上下载了 Xcode 15 beta。

我想将我的 View Model 类从 @ObservableObject 迁移到 @Observable

然而,当我尝试添加新的属性时,我收到了一个错误消息:

@Observable final class ViewModel {
    
}

未知属性 'Observable'

我删除了原始的 Xcode 版本,然后在运行 sudo xcode-select --reset 后使用 Xcode-select 选择了 beta 版本。似乎什么都不起作用。
我还手动安装了 Xcode 15 的命令行工具。

当运行 swift -version 时,我得到以下输出:

swift-driver version: 1.82.2 Apple Swift version 5.9 (swiftlang-5.9.0.114.6 clang-1500.0.27.1)
Target: arm64-apple-macosx14.0

我漏掉了什么吗?在 Xcode 15 的首个 beta 版中还不支持这个吗?

英文:

I just downloaded Xcode 15 beta on my developer machine running the first beta of macOS Sonoma.

I want to port my View Model class from @ObservableObject to @Observable.

However, when I try to add the new attribute, I get an error message:

@Observable final class ViewModel {
    
}

> Unknown attribute 'Observable'

I deleted the original Xcode version, I selected the beta version using Xcode-select after running sudo xcode-select --reset. Nothing seems to work.
I also installed the command line tools for Xcode 15 manually.

When running swift -version, I get the following output:

swift-driver version: 1.82.2 Apple Swift version 5.9 (swiftlang-5.9.0.114.6 clang-1500.0.27.1)
Target: arm64-apple-macosx14.0

What am I missing? Is this not supported yet in the first beta of Xcode 15?

答案1

得分: 8

你需要导入Observation框架:

import Observation

@Observable final class ViewModel {
   // 更多代码在此
}

来源:@Observable and other SwiftUI Macros

英文:

You need to import the Observation framework:

import Observation

@Observable final class ViewModel {
   // more code here
}

Source: @Observable and other SwiftUI Macros

huangapple
  • 本文由 发表于 2023年6月11日 23:51:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定