14

I have a swift protocol IModelIdProvider in the MyProjectName folder. I have created an objective-c unit test class named RemoteModelFactoryTest in the MyProjectNameTests folder. To do some tests, RemoteModelFactoryTest must implement the IModelIdProvider protocol.

When I #import "MyProjectName-Swift.h" to use my swift protocol in my objective-c class, I get a file not found.

If I change the #import instruction to #import "MyProjectNameTests-Swift.h", the header is found but not my protocol (it's defined in the MyProjectName project, not in the MyProjectNameTests project).

Is there something special to do in the *Tests projects to use Swift code ?

Morniak
  • 855
  • 1
  • 12
  • 31
  • 2
    Is the Swift file targeted towards the test bundle? – SushiGrass Jacob Jun 23 '14 at 14:14
  • The current swift documentation is either wrong or there is a bug in Xcode. In order for the file to be found, you must define "Product Module Name" in the build settings - set it to "MyProjectName" and your import should work. – Abhi Beckert Jun 23 '14 at 14:19
  • @AbhiBeckert I've tried changing the Product Module Name as you suggest (see [here](http://stackoverflow.com/questions/28114772/cant-use-swift-classes-inside-objective-c-unit-test)) but this did not help so far. Did you actually try the recipe? – Drux Jan 23 '15 at 16:58
  • @Morniak Have you been able to resolve this (see [here](http://stackoverflow.com/questions/28114772/cant-use-swift-classes-inside-objective-c-unit-test))? – Drux Jan 23 '15 at 17:01
  • @Drux this question is 6 months old and Swift has changed a lot since then, so it might be out of date. What I posted did work for me, months ago. I don't know how it works now. – Abhi Beckert Jan 25 '15 at 05:05
  • @AbhiBeckert I've by now also been able to resolve this (see [here](http://stackoverflow.com/questions/28114772/cant-use-swift-classes-inside-objective-c-unit-test)). – Drux Jan 25 '15 at 05:38

2 Answers2

1

I know its extra hacky and everything, but what I did and it worked was to copy public headers from generated "Project-Swift.h" and paste whats needed to .m file of test.

So I copied something like this:

SWIFT_CLASS("_TtC10Inventorum11ImageEntity")
@interface ImageEntity : NSObject <FICEntity>
@property (nonatomic, copy) NSString * imageID;
@property (nonatomic) NSURL * retinaImageURL;
@property (nonatomic) NSURL * nonRetinaimageURL;
@property (nonatomic, readonly) NSURL * imageURL;
@property (nonatomic, readonly, copy) NSString * UUID;
@property (nonatomic, readonly, copy) NSString * sourceImageUUID;
@property (nonatomic) RACSignal * rac_signalForImage;
- (instancetype)init OBJC_DESIGNATED_INITIALIZER;
- (NSURL *)sourceImageURLWithFormatName:(NSString *)formatName;
- (FICEntityImageDrawingBlock)drawingBlockForImage:(UIImage *)image withFormatName:(NSString *)formatName;
- (void)objc_setType:(NSString *)type;
@end

And on top of this I have also copied all the macros for SWIFT_CLASS etc. My tests are building and passing testing Swift code.

BTW. Dont forget to put public on tested classes and methods in Swift.

Bartosz Hernas
  • 1,060
  • 9
  • 17
-1

To import a set of Objective-C files in the same framework target as your Swift code, you’ll need to import those files into the Objective-C umbrella header for the framework.

To import Swift code into Objective-C from the same framework

Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. Import the Swift code from that framework target into any Objective-C .m file within that framework target using this syntax, and substituting the appropriate names...

https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/buildingcocoaapps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75

Nikolai Nagornyi
  • 1,233
  • 2
  • 11
  • 25
  • 2
    I don't think this will resolve the problem that @Morniak has. He said that he wants to use a Swift file from `Project-Target` into an Obj-C Unit test in `ProjectTest-Target`. – Jean Lebrument Jun 25 '14 at 12:47