将@objc属性用于不从Objective-C类继承的Swift类

时间:2022-09-24 23:12:14

The following paragraph from the section on Interoperability of the Using Swift with Cocoa and Objective-C (Swift 2.1) documentation seems to suggest that there is a way to use a Swift class that does not inherit from an Objective-C class for interoperability.

使用Swift与Cocoa和Objective-C(Swift 2.1)文档的互操作性一节中的以下段落似乎表明,有一种方法可以使用不从Objective-C类继承互操作性的Swift类。

When you create a Swift class that descends from an Objective-C class, the class and its members—properties, methods, subscripts, and initializers that are compatible with Objective-C—are automatically available from Objective-C. In some cases, you need finer grained control over how your Swift API is exposed to Objective-C. You can use the @objc attribute if your Swift class doesn’t inherit from an Objective-C class, or if you want to change the name of a symbol in your interface as it’s exposed to Objective-C code.

当您创建一个源自Objective-C类的Swift类时,可以从Objective-C自动获得与Objective-C兼容的类及其成员属性,方法,下标和初始化程序。在某些情况下,您需要更精细地控制Swift API如何向Objective-C公开。如果您的Swift类不从Objective-C类继承,或者您希望在接口中更改符号的名称(因为它暴露给Objective-C代码),则可以使用@objc属性。

And I attempted the following:

我尝试了以下方法:

import Foundation

@objc class FooBar {
    @objc var name: String;
    init() {
        name = "Hello World!"
    }
}

But unfortunately, it gives a compilation error: Only classes that inherit from NSObject can be declared @objc

但不幸的是,它给出了编译错误:只能从NSObject继承的类声明@objc

Am I misinterpreting something?

我误解了什么吗?

1 个解决方案

#1


1  

Take a look at this thread on the Apple Developer Forums.

看一下Apple Developer Forums上的这个主题。

Basically what you say was possible in Xcode <=6, but removed on Xcode 7

基本上你所说的Xcode <= 6是可能的,但在Xcode 7上删除了

Pretty much yes. @objc on Swift-rooted classes never quite behaved like an NSObject-rooted class, leading to various weirdness in the generated header and at runtime. You can still treat any Swift class instance as an AnyObject, mark methods and properties on a Swift class as @objc, and conform to Objective-C protocols; the class just isn't exposed in the generated header and doesn't default to having its members available in Objective-C.

差不多是的。 @objc对Swift-rooted类的行为从来没有像NSObject的root类那样,导致生成的头和运行时出现各种奇怪现象。您仍然可以将任何Swift类实例视为AnyObject,将Swift类上的方法和属性标记为@objc,并符合Objective-C协议;该类只是未在生成的头中公开,并且不默认在Objective-C中提供其成员。

#1


1  

Take a look at this thread on the Apple Developer Forums.

看一下Apple Developer Forums上的这个主题。

Basically what you say was possible in Xcode <=6, but removed on Xcode 7

基本上你所说的Xcode <= 6是可能的,但在Xcode 7上删除了

Pretty much yes. @objc on Swift-rooted classes never quite behaved like an NSObject-rooted class, leading to various weirdness in the generated header and at runtime. You can still treat any Swift class instance as an AnyObject, mark methods and properties on a Swift class as @objc, and conform to Objective-C protocols; the class just isn't exposed in the generated header and doesn't default to having its members available in Objective-C.

差不多是的。 @objc对Swift-rooted类的行为从来没有像NSObject的root类那样,导致生成的头和运行时出现各种奇怪现象。您仍然可以将任何Swift类实例视为AnyObject,将Swift类上的方法和属性标记为@objc,并符合Objective-C协议;该类只是未在生成的头中公开,并且不默认在Objective-C中提供其成员。