3

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.

9
  • My guess: The file "BDVTestView.swift" has not been added to the target (check the "Target Membership" checkbox in the file inspector. Commented Jul 24, 2014 at 20:11
  • Thanks for the response -- however, every swift file has the correct target checked in file inspector underneath Target Membership Commented Jul 24, 2014 at 21:48
  • Possibly stupid, but I have to ask. Did you try to build at all? I have had several problems with interoperability code being reported as errors, but when I build everything compiles normally. Commented Jul 27, 2014 at 2:21
  • Yeah it usually appears as a build error but goes away while still in the build process. Commented Jul 27, 2014 at 2:23
  • Make sure Defines Module is set to YES for your project (not only your target) and the Project Module Name is set to the correct name. Commented Jul 28, 2014 at 9:24

2 Answers 2

6
+50

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

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

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.