-1

The situation is a little complicated. I have a Swift project, and I have imported some Objective-C files into this project. Now, I want to use some Swift classes in these Objective-C files. How to do that?

BaseZen
  • 8,650
  • 3
  • 35
  • 47
yong ho
  • 3,892
  • 9
  • 40
  • 81

1 Answers1

2

It seems this reference covers everything:

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

Specifically:

Importing Swift into Objective-C

When you import Swift code into Objective-C, you rely on an Xcode-generated header file to expose those files to Objective-C. This automatically generated file is an Objective-C header that declares the Swift interfaces in your target. It can be thought of as an umbrella header for your Swift code. The name of this header is your product module name followed by adding "-Swift.h". (You’ll learn more about the product module name later, in Naming Your Product Module.)

Example:

Foo.swift in product named ExampleProduct

@objc public class Bar: NSObject {
    var x: Int = 3
}

Blah.m in same product:

#import <Foundation/Foundation.h>
#import <ExampleProduct-Swift.h>

void doStuff() {
    Bar *y = [[Bar alloc] init];
    printf("%ld\n", (long)y.x);
}

It's even easy to call doStuff() from a Swift file if you create an Objective-C header file that defines the function prototype and then import that header file in the bridging header.

There's no limit to jumping back and forth.

Based on the comments it looks like you're having trouble importing the reverse bridging header.

Try this in terminal to ensure you're naming the file correctly:

% cd ~/Library/Developer/Xcode; find . -name '*-Swift.h'

I get (scroll all the way to the right):

./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProduct.build/DerivedSources/ExampleProduct-Swift.h
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProduct.build/Objects-normal/x86_64/ExampleProduct-Swift.h
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProductTests.build/DerivedSources/ExampleProductTests-Swift.h
./DerivedData/ExampleProduct-avoxifngmebkkqgndldocildsfcm/Build/Intermediates/ExampleProduct.build/Debug-iphonesimulator/ExampleProductTests.build/Objects-normal/x86_64/ExampleProductTests-Swift.h

Also, potentially a dup of:

How to call Objective-C code from Swift

With 326 upvotes that's worth studying!

Community
  • 1
  • 1
BaseZen
  • 8,650
  • 3
  • 35
  • 47
  • I tried. It says the #import file not found. My situation is like this: I want to import __swift class__ into a __objective-c file__ which both of them belongs to a **swift project**. Your solution is for import swift class into a objective-c project, right? – yong ho Aug 05 '15 at 07:42
  • 1
    Please post your code. My solution was within a Swift project. Xcode silently creates **ExampleProduct-Swift.h** as soon as your target is mixed Obj-C/Swift (i.e. as soon as you add an Obj file to the Swift project.) Is that the failing line? Did you get the name of your product correct? Screenshot perhaps? – BaseZen Aug 05 '15 at 16:28
  • Edited to address your import issue. – BaseZen Aug 05 '15 at 16:35