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

前端之家收集整理的这篇文章主要介绍了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.

原文链接:https://www.f2er.com/iOS/332863.html

猜你在找的iOS相关文章