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

**URL:** <https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348>\
**Category:** Package Manager\
**Created:** [July 26, 2019, 1:18pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348 "2019-07-26T13:18:46Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![miguel.cabeca](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miguel.cabeca/32/8542_2.png) [@miguel.cabeca](https://forums.swift.org/u/miguel.cabeca)\
**Post date:** [July 26, 2019, 1:18pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/1 "2019-07-26T13:18:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Aciid](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aciid/32/429_2.png) [@Aciid](https://forums.swift.org/u/Aciid)\
**Post date:** [July 26, 2019, 4:15pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/2 "2019-07-26T16:15:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![miguel.cabeca](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/miguel.cabeca/32/8542_2.png) [@miguel.cabeca](https://forums.swift.org/u/miguel.cabeca)\
**Post date:** [July 26, 2019, 6:50pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/3 "2019-07-26T18:50:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![TheDeanosaurus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thedeanosaurus/32/9098_2.png) [@TheDeanosaurus](https://forums.swift.org/u/TheDeanosaurus)\
**Post date:** [September 25, 2019, 8:43pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/4 "2019-09-25T20:43:20Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Aciid](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/aciid/32/429_2.png) [@Aciid](https://forums.swift.org/u/Aciid)\
**Post date:** [September 25, 2019, 8:53pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/5 "2019-09-25T20:53:37Z")

</div>

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](https://github.com/apple/swift-package-manager/pull/2341) couple of days ago.

---

<div class="post-metadata">

**Author:** ![TheDeanosaurus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thedeanosaurus/32/9098_2.png) [@TheDeanosaurus](https://forums.swift.org/u/TheDeanosaurus)\
**Post date:** [September 25, 2019, 9:21pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/6 "2019-09-25T21:21:41Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![blin4444](https://avatars.discourse-cdn.com/v4/letter/b/5daacb/32.png) [@blin4444](https://forums.swift.org/u/blin4444)\
**Post date:** [October 20, 2019, 4:12pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/7 "2019-10-20T16:12:13Z")

</div>

I'm having the same issue.

---

<div class="post-metadata">

**Author:** ![TheDeanosaurus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thedeanosaurus/32/9098_2.png) [@TheDeanosaurus](https://forums.swift.org/u/TheDeanosaurus)\
**Post date:** [October 23, 2019, 7:12pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/8 "2019-10-23T19:12:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![njvenn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/njvenn/32/12893_2.png) [@njvenn](https://forums.swift.org/u/njvenn)\
**Post date:** [February 16, 2020, 4:21pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/9 "2020-02-16T16:21:01Z")

</div>

Just fyi, this issue is resolved in Xcode 11.4.

---

<div class="post-metadata">

**Author:** ![dlafferty](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@dlafferty](https://forums.swift.org/u/dlafferty)\
**Post date:** [February 24, 2020, 2:52pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/10 "2020-02-24T14:52:14Z")

</div>

If the SwiftPM is responsible for generating the header file, how do you reference it from an Xcode project? Is this done automatically?

I ask, because I see a number of examples online where a Run Script step is required to get ahold of the the header files, and I'm wondering if this is available when using SwiftPM.

---

<div class="post-metadata">

**Author:** ![TheDeanosaurus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thedeanosaurus/32/9098_2.png) [@TheDeanosaurus](https://forums.swift.org/u/TheDeanosaurus)\
**Post date:** [February 24, 2020, 3:33pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/11 "2020-02-24T15:33:39Z")

</div>

It should just be implicitly through the module import. The reason those kind of scripts exist I believe is because in the past those compatibility header files _weren't_ generated. Now if I have Swift module FooSwift and import it into my .m ObjC file in FooObjC like so `@import FooSwift;` it should see everything made available to ObjC from FooSwift. Someone else can correct me if I'm wrong I've just done a few preliminary tests with a mix of library types.

---

<div class="post-metadata">

**Author:** ![dlafferty](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@dlafferty](https://forums.swift.org/u/dlafferty)\
**Post date:** [February 24, 2020, 3:39pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/12 "2020-02-24T15:39:12Z")

</div>

I had my 'C' head on, and I was using `#import` instead of `@import`.

Thanks for the suggestion!

Update: on further inspection, it seems that I was mislead by the documentation, which recommends using #import for in project and framework Swift code. See [Apple Developer Documentation](https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c) . I guess they need a special section for Swift Packages

---

<div class="post-metadata">

**Author:** ![AlexSedykh](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@AlexSedykh](https://forums.swift.org/u/AlexSedykh)\
**Post date:** [August 2, 2020, 6:24am UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/13 "2020-08-02T06:24:39Z")

</div>

> [@miguel.cabeca](#):
>
> 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?

Does it work in Xcode11? Because I have some problem. I have class defined in Package

> ```
> @objc public class ObjCCountry: NSObject {
> @objc public let id: Int
> @objc public let title: String
> @objc public func getTitle() -> String { title }
> @objc public func printTitle() { 
> print("title: \(title)")
> }
> public init(id: Int, title: String) {
> self.id = id
> self.title = title
> }
> }
> 
> ```

And no one of these properties and methods I could not see in .mm file

> ```
> #import "SearchCountries-Swift.h"
> 
> ```

```
     ...

```

> ```
> ObjCCountry *z = [gate getCountry];
> NSLog(@"run, z: %@", z.title); // Property 'title' cannot be found in forward class object 'ObjCCountry'
> 
> ```

While Option+Click on `ObjCCountry` and Jump to Definition sends me to the source code in the package

---

<div class="post-metadata">

**Author:** ![TheDeanosaurus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thedeanosaurus/32/9098_2.png) [@TheDeanosaurus](https://forums.swift.org/u/TheDeanosaurus)\
**Post date:** [March 22, 2021, 3:43pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/14 "2021-03-22T15:43:39Z")

</div>

Sorry to revive a dead thread but I need to correct what I said previously...

> It should just be implicitly through the module import. The reason those kind of scripts exist I believe is because in the past those compatibility header files _weren't_ generated. Now if I have Swift module FooSwift and import it into my .m ObjC file in FooObjC like so `@import FooSwift;` it should see everything made available to ObjC from FooSwift. Someone else can correct me if I'm wrong I've just done a few preliminary tests with a mix of library types.

This ONLY works if the library _consuming_ the Swift (or ObjC) package has the package product linked EXPLICITLY in the consuming library's `Link Binary With Libraries` phase. This is a huge issue because then now if you are like us and have many many libraries within your application then they would all have to link the library directly which **only** works if you make the swift package product's type `.dynamic` as to avoid duplicate symbols.

Then as you continue to convert things to SPM you have more and more dynamically linked things making app launch times horrendous.

If anyone has any insight on how to import a Swift (ObjC) package into an ObjC static library that is a child project of the main app project without explicitly linking it to the static library let me know...

TLDR: Can't use `@import SomePackageLibrary` in ObjC code in static library _without explicit linking_, and can't `import SomeObjCPackageLibrary` in swift code in static library _without explicit linking_.

---

<div class="post-metadata">

**Author:** ![1oo7](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/1oo7/32/17598_2.png) [@1oo7](https://forums.swift.org/u/1oo7)\
**Post date:** [June 8, 2021, 10:17pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/15 "2021-06-08T22:17:18Z")

</div>

> [@TheDeanosaurus](#):
>
> Then as you continue to convert things to SPM you have more and more dynamically linked things making app launch times horrendous.
> 
> If anyone has any insight on how to import a Swift (ObjC) package into an ObjC static library that is a child project of the main app project without explicitly linking it to the static library let me know...
> 
> TLDR: Can't use `@import SomePackageLibrary` in ObjC code in static library _without explicit linking_ , and can't `import SomeObjCPackageLibrary` in swift code in static library _without explicit linking_ .

The problem is, **you shouldn't convert things into Swift packages.** It is not suitable as a replacement for Xcode project files to declare local modules in many cases—this was simply **not** a use-case that SPM was designed to handle, and Apple has put little to no priority on making SPM work for this use case.

I'd love to be proven wrong, but literally every single time I've used a Swift Package instead of an Xcode project file for a local target, I've later come to seriously regret it.

---

<div class="post-metadata">

**Author:** ![1oo7](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/1oo7/32/17598_2.png) [@1oo7](https://forums.swift.org/u/1oo7)\
**Post date:** [June 8, 2021, 10:18pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/16 "2021-06-08T22:18:58Z")

</div>

> [@TheDeanosaurus](#):
>
> TLDR: Can't use `@import SomePackageLibrary` in ObjC code in static library _without explicit linking_ , and can't `import SomeObjCPackageLibrary` in swift code in static library _without explicit linking_ .

AND you can't @import a Swift package target in an ObjC header if that header is part of a module that's defined using an Xcode project file—making SPM worse than useless as a replacement for Xcode project files.

---

<div class="post-metadata">

**Author:** ![pfandrade](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pfandrade/32/22031_2.png) [@pfandrade](https://forums.swift.org/u/pfandrade)\
**Post date:** [March 1, 2022, 1:38pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/17 "2022-03-01T13:38:26Z")

</div>

You can still import your Swift module from ObjC without having to add it to the `Link Binary With Libraries` by adding `-fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/FooSwift.modulemap` to the Other C Flags of your ObjC target. You still need to have FooSwift in the target's dependencies to make sure to module map is generated.

---

<div class="post-metadata">

**Author:** ![TheDeanosaurus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/thedeanosaurus/32/9098_2.png) [@TheDeanosaurus](https://forums.swift.org/u/TheDeanosaurus)\
**Post date:** [March 1, 2022, 2:30pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/18 "2022-03-01T14:30:00Z")

</div>

Hey thanks for the reply and welcome!

While this does make the import and building it work, unfortunately adding `FooSwift` as a dependency requires it live within the same project file as the ObjC target that is dependent on it which in our case is unfeasible (we have 80ish targets all in their own sub-projects of the main app target project).

Perhaps adding the package as a dependency to the main app target will work. I'll play around with it a bit and see. I've never been able to get it to import without linking though so that's new. Thanks!

---

<div class="post-metadata">

**Author:** ![pfandrade](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/pfandrade/32/22031_2.png) [@pfandrade](https://forums.swift.org/u/pfandrade)\
**Post date:** [March 2, 2022, 2:16pm UTC](https://forums.swift.org/t/using-a-swift-package-in-a-mixed-swift-and-objective-c-project/27348/19 "2022-03-02T14:16:03Z")

</div>

If you can figure out where those Swift packages are generating their modulemaps, you should be able to replace GENERATED\_MODULEMAP\_DIR with some other path.
