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.

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...