bdev
bdev

Reputation: 2056

Including Swift files in Objective-C

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

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:

#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 *view = [BVDTestView new];

Use of undeclared identifier 'view'

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

Upvotes: 3

Views: 1880

Answers (2)

Mahmud Ahsan
Mahmud Ahsan

Reputation: 2055

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.

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

}

Upvotes: 0

jtbandes
jtbandes

Reputation: 118761

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

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.

Upvotes: 6

Related Questions