最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Do you actually need to use "import Observation" when using the @Observable macro in SwiftUI - Stack Overflow

programmeradmin3浏览0评论

I have used @Observable extensively in a side project to learn more about it. It all seems to be working exactly as expected. But the other day I was watching a Youtube dev show something and noticed that he used import Observation and thus his @Observable was purple.

This may be a dumb question and maybe I have just been lucky that @Obrservable has thus for worked in my small app. I have looked around for an answer and can find almost no-one showing that they import Observation when using the macro.

Do you really need to import Observation to make use of the @Observable macro?

I have used @Observable extensively in a side project to learn more about it. It all seems to be working exactly as expected. But the other day I was watching a Youtube dev show something and noticed that he used import Observation and thus his @Observable was purple.

This may be a dumb question and maybe I have just been lucky that @Obrservable has thus for worked in my small app. I have looked around for an answer and can find almost no-one showing that they import Observation when using the macro.

Do you really need to import Observation to make use of the @Observable macro?

Share Improve this question asked Mar 26 at 18:19 DiltsDilts 354 bronze badges 1
  • 1 No, you don't need to import Observation to use the @Observable macro, but sometimes you have to if you want to expand the macro to see the code it generates, for example. – Andrei G. Commented Mar 26 at 20:41
Add a comment  | 

1 Answer 1

Reset to default 1

No, you do not need to say import Observation to be able to use @Observable. Observation is exported by Foundation. In the .swiftinterface files of Foundation, it says

@_exported import Observation

So as long as you import Foundation in some way, directly or indirectly, you will be able to use things declared in Observation.

This is similar to how you can use things in Foundation (e.g. DateFormatter) when you have only imported SwiftUI, but not Foundation directly.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论