Using a Swift Package in a mixed Swift and Objective-C project

Hello,

I apologize in advance if this question has been asked before (I searched the forum but did not find anything).

On a brand new project, I created swift sources and Objective-C sources and made sure they worked together. I then proceeded to create a new local Swift Package in Xcode 11 beta 4 with a single Swift file that defined a NSObject subclass with a string inside.
When I tried to use the package class from the project Swift code, it went fine, as expected, after importing the module. I then tried using the package class from the project Objective-C code and no matter what, I could not make it work.

Is this expected behavior? If so, why? And why doesn't a '#import "Package-Swift.h"' or 'import <Package/Package-Swift.h>' in the .m file where I want to use the package code solve the problem?

Thank you for the clarification.

3 Likes

This is a known issue. You can't import a Swift package target into an Objective-C target right now.

1 Like

Thank you for the quick response.
Is there a Jira ticket that we can follow regarding a future implementation of this feature? Is there a timeframe for it?

3 Likes

Clearly "Swift" packages are meant to be used with Swift code and all new development should be done in Swift but this makes it really difficult to add features into existing projects where the code path runs through ObjC code. I'm just curious as to why this doesn't "just work"; I know nothing about the internals of SPM.

Are there any details about this or a JIRA task associated with it? We're looking to migrate to using packages sometime in the future and we're trying to plan a path forward, not being able to import Swift targets into ObjC targets in a large roadblock to that.

This required some work in the build system. At a higher level, SwiftPM needed to ask the Swift compiler to emit an ObjC compatibility header file which can be used by the ObjC targets.

I landed support for this in master couple of days ago.

4 Likes

That's amazing! I guess two follow up questions:
Any idea on availability within Xcode? (sort of apple-specific question I guess)
Also the PR mentions it being only for macOS is there a purpose for this? Our case would be iOS/iPadOS.

Edit: I just realized that comment may be pertaining to the build agent, as in, only SPM implementations built using macOS would see this functionality since it's the only place ObjC is accessible. Is that correct to say?

2 Likes

I'm having the same issue.

It looks like it has already been fixed but isn't available yet (likely at least until after Nov 5 when 5.2 is cut). Whatever Xcode version first adopts 5.2 I imagine would have this functionality.

The real question is is it possible to download the latest toolkit and test this functionality within Xcode now?

Terms of Service

Privacy Policy

Cookie Policy