Mathieu Tozer
Mathieu Tozer

Reputation: 278

Swift framework imported into Objc-C binary does not generate Obj-c header correctly, missed @import Cocoa;

I am building a Mac OS X app (binary). Skit is the Swift framework, and Pal is the Application which contains the .m file. The compiler cannot find NSArray or NSDictionary, which means that the compiler can't find the Foundation module. If I go to the error in Pal-Swift.h (which is generated) and add the @import Foundation or @import Cocoa manually, the binary compiles fine and I am able to use the classes in the framework from objc as expected, and I can typically keep iterating until I need to do a clean build.

The error looks like this.

    While building module 'SKit' imported from /Users/mtozer/Library/Developer/Xcode/DerivedData/Pal-dznqzplhixgqpgduvvxjqiknvhrr/Build/Intermediates/Pal.build/Debug/Pal.build/DerivedSources/Pal-Swift.h:90:
In file included from <module-includes>:1:
/Users/mtozer/Library/Developer/Xcode/DerivedData/Pal-dznqzplhixgqpgduvvxjqiknvhrr/Build/Products/Debug/SPluginKit.framework/Headers/SKit-Swift.h:97:26: error: expected a type
- (void)executeCommands:(NSArray * __nonnull)commands;
                         ^
/Users/mtozer/Library/Developer/Xcode/DerivedData/Pal-dznqzplhixgqpgduvvxjqiknvhrr/Build/Products/Debug/SKit.framework/Headers/SKit-Swift.h:115:4: error: expected a type
+ (NSDictionary<NSString *, id <SPlugin>> * __nonnull)loadPluginsInDirectory:(NSString * __nonnull)directory sAPI:(id <SAPI> __nonnull)sAPI;
   ^
2 errors generated.
In file included from /Users/mtozer/Pal/Pal/AppDelegate.m:43:
/Users/mtozer/Library/Developer/Xcode/DerivedData/Pal-dznqzplhixgqpgduvvxjqiknvhrr/Build/Intermediates/Pal.build/Debug/Pal.build/DerivedSources/Pal-Swift.h:90:9: fatal error: could not build module 'SKit'
@import SKit;
 ~~~~~~~^~~~~~~~~~~~~~~~~~
3 errors generated.

Upvotes: 1

Views: 239

Answers (2)

Victor Gabana
Victor Gabana

Reputation: 26

Remember to prepend @objc to the classes and protocols declaration in Swift.

@objc public class YourClassName: NSObject { ... }

@objc public protocol YourProtocolName { ... }

If the problem persists then try appending : class at the end of the protocols used as Swift protocols can be implemented not only by classes as happens in Objc.

@objc public protocol YourProtocolName: class { ... }

Upvotes: 0

Mathieu Tozer
Mathieu Tozer

Reputation: 278

I'm fairly convinced this is a compiler bug, but then again I am using BUCK which does not officially support swift.

Simply putting an empty extension in my Framework's source code caused the generator to put the foundation dependency correctly in the header

extension NSArray {

}

Upvotes: 2

Related Questions