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.

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
bdev
  • 2,060
  • 5
  • 24
  • 32
  • My guess: The file "BDVTestView.swift" has not been added to the target (check the "Target Membership" checkbox in the file inspector. – Martin R Jul 24 '14 at 20:11
  • Thanks for the response -- however, every swift file has the correct target checked in file inspector underneath Target Membership – bdev Jul 24 '14 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. – Logan Jul 27 '14 at 02:21
  • Yeah it usually appears as a build error but goes away while still in the build process. – bdev Jul 27 '14 at 02: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. – Sulthan Jul 28 '14 at 09:24
  • @Sulthan Defines Module is set to YES for both the project and target. The Project Module Name is TestApp and I'm using TestApp-Swift.h (which was created at build). Still running into the same error. – bdev Jul 28 '14 at 17:32
  • http://stackoverflow.com/questions/24206732/cant-use-swift-classes-inside-objective-c – Andrew Jul 28 '14 at 21:31
  • @SantaClaus - looks like a bit of a different issue that the OP was experiencing. – bdev Jul 28 '14 at 22:09
  • @bdev I thought so, but I decided to post it anyway with the hope that it might help somebody. – Andrew Jul 28 '14 at 22:10

2 Answers2

6

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.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
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")
}

}

Mahmud Ahsan
  • 1,755
  • 19
  • 18