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

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

概述当我在类的扩展中声明静态参数时,我必须在变量之前编写@nonobjc @nonobjc static let test = "test" 有时我必须在方法之前编写@objc,所以在swift中使用@objc和@nonobjc. 任何人都可以帮我解决这个问题吗? 这在 Apple’s official documentation中解释了Objective-C – Swift互 *** 作性: When yo 当我在类的扩展中声明静态参数时,我必须在变量之前编写@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 – 当@objc和@nonobjc在swift中编写方法和变量之前?全部内容,希望文章能够帮你解决ios – 当@objc和@nonobjc在swift中编写方法和变量之前?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1102224.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存