包括Objective-C中的Swift文件。

时间:2022-09-07 08:26:14

I'm attempting to include a swift class in an objective-c project. The swift class inherits from UIView and looks like this:

我正在尝试在objective-c项目中包含一个swift类。swift类继承自UIView,如下所示:

class BVDTestView: UIView {
...
}

Note that I do not include @objc because the swift class inherits from UIView. In an objective-c implementation file, I import the umbrella swift header:

注意,我不包含@objc,因为swift类继承自UIView。在objective-c实现文件中,我导入了雨伞swift报头:

#import "TestApp-Swift.h"

I see that this file is created when I build, but I do not see any references to BVDTestView in it (I would think that I would). When I try to create an instance of the swift view I get the error:

我看到这个文件是在构建时创建的,但是我在其中没有看到任何对BVDTestView的引用(我认为我会这样做)。当我尝试创建swift视图的实例时,我得到了错误:

BVDTestView *view = [BVDTestView new];

Use of undeclared identifier 'view'

使用未声明的标识符'view'

Any thoughts? I'm on Xcode 6 beta 4.

任何想法吗?我在Xcode 6 beta 4上。

2 个解决方案

#1


6  

I ran into this recently. As of Beta 4, Swift has access specifiers, and

我最近遇到了这个问题。在Beta 4中,Swift有访问说明符和

By default, all entities have internal access.

默认情况下,所有实体都具有内部访问权限。

You need to explicitly mark the class as public class BVDTestView ... for it to appear in the generated header.

您需要显式地将类标记为公共类BVDTestView…使它出现在生成的标题中。

#2


0  

For my case, when I created new Obj-C based project and want to integrate Swift code faced no problem, without specifiers like "public" it works. But one of my existing Obj-C based project when I test to add swift code I found the problem and solved it by providing "Public" access specifier in both class and function.

在我的例子中,当我创建新的基于object - c的项目并想要集成Swift代码时,如果没有像“public”这样的说明符,就没有问题。但是在我的一个基于objc的项目中,当我测试添加swift代码时,我发现问题并通过在类和函数中提供“公共”访问说明符来解决它。

@objc public class Test: NSObject{
 @objc public func test(){
    println("test")
}

}

}

#1


6  

I ran into this recently. As of Beta 4, Swift has access specifiers, and

我最近遇到了这个问题。在Beta 4中,Swift有访问说明符和

By default, all entities have internal access.

默认情况下,所有实体都具有内部访问权限。

You need to explicitly mark the class as public class BVDTestView ... for it to appear in the generated header.

您需要显式地将类标记为公共类BVDTestView…使它出现在生成的标题中。

#2


0  

For my case, when I created new Obj-C based project and want to integrate Swift code faced no problem, without specifiers like "public" it works. But one of my existing Obj-C based project when I test to add swift code I found the problem and solved it by providing "Public" access specifier in both class and function.

在我的例子中,当我创建新的基于object - c的项目并想要集成Swift代码时,如果没有像“public”这样的说明符,就没有问题。但是在我的一个基于objc的项目中,当我测试添加swift代码时,我发现问题并通过在类和函数中提供“公共”访问说明符来解决它。

@objc public class Test: NSObject{
 @objc public func test(){
    println("test")
}

}

}