ios – 当@objc和@nonobjc在swift中编写方法和变量之前?

当我在类的扩展中声明静态参数时,我必须在变量之前编写@nonobjc
@nonobjc static let test = "test"

有时我必须在方法之前编写@objc,所以在swift中使用@objc和@nonobjc.

任何人都可以帮我解决这个问题吗?

解决方法

这在 Apple’s official documentation中解释了Objective-C – Swift互操作性:

When you use the @objc(name) attribute on a Swift class,the class is
made available in Objective-C without any namespacing. As a result,
this attribute can also be useful when migrating an archivable
Objective-C class to Swift. Because archived objects store the name of
their class in the archive,you should use the @objc(name) attribute
to specify the same name as your Objective-C class so that older
archives can be unarchived by your new Swift class.

Conversely,Swift also provides the @nonobjc attribute,which makes a
Swift declaration unavailable in Objective-C. You can use it to
resolve circularity for bridging methods and to allow overloading of
methods for classes imported by Objective-C. If an Objective-C method
is overridden by a Swift method that cannot be represented in
Objective-C,such as by specifying a parameter to be a variable,that
method must be marked @nonobjc.

总而言之,如果要在没有命名空间的情况下将Swift属性公开给Objective-C,请使用@objc.如果要保持属性可用并且只能在Swift代码中访问,请使用@nonobjc.

相关文章

当我们远离最新的 iOS 16 更新版本时,我们听到了困扰 Apple...
欧版/美版 特别说一下,美版选错了 可能会永久丧失4G,不过只...
一般在接外包的时候, 通常第三方需要安装你的app进行测...
前言为了让更多的人永远记住12月13日,各大厂都在这一天将应...