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

I have used @Observable extensively in a side project to learn more about it. It all seems to be workin

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744132761a4559914.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信