0

Is there a way I can use package.swift in an Xcode project? I have written a package.swift file which is being read/ understood by swift build, but it looks like Xcode still doesn't know about this file. I don't want to use the built-in Swift Packages tab because I don't know how Xcode handles that (e.g. where do the contents get version controlled?).

I have a feeling I'm asking something impossible, as using a project package.swift may be mutually exclusive to XCode.

I did read Use swift package manager on existing xcode project but the answer here recommended using the Xcode swift package manager feature, which is what I am trying not to use.

Ben Butterworth
  • 6,030
  • 1
  • 28
  • 48

2 Answers2

0

To use a Swift package as an Xcode project, just open the Package.swift directly. Xcode will create a workspace (in comments, I said a project, but it's a workspace) in the package's .swiftpm directory. There won't be any project settings, because you do that by editing the Package.swift file, and files are always sorted. Other than that, it's pretty much like working in an Xcode project, including debugging and testing.

Of course there are the other modes of using a package

You can just drag the Package.swift file into your Xcode project, if it's a local package. You'll also need to add it as a dependency in your General settings, just as you would any framework. That's not the same as the "Add Package Dependency" feature. It's the same Project Settings -> Target -> Frameworks & Libraries that you use for any traditional framework.

If you do it that way, the package is part of the Xcode project.

If the package is already under it's own git repo, Xcode will prompt you which repo you want to do things like "push" or whatever, though commit just applies to whatever repo the files being committed belong to... so if you change some app code and package code, and commit them both at the same time, the app code would go into the app's repo, and the package code would go to the package's repo.

If the package isn't under source control and you relocate it to your project folder, I think the project's repo will take responsibility for those sources, though you might have to explicitly check them in Xcode's commit (or git add from the command line).

I'm not sure how it handles it, if it's in an unrelated folder and not in its own repo.

If it's a remote package, which is different, you need to add it as a package dependency. No getting around that for remote packages.

Chip Jarred
  • 1,066
  • 2
  • 4
  • I mean an explicit package.swift to describe my targets, not a dependency which I want to install. – Ben Butterworth Mar 26 '21 at 18:28
  • If it's a local package (that is, just dragged into your project from a local folder), then it's part of your project... it's not installing anything. It's like having a traditional framework as a separate target in your project. You can edit the package source, check-in, etc... without having to open it separately, which you cannot do with a remote (installed) package dependency. – Chip Jarred Mar 26 '21 at 19:32
  • I don't want a package.swift for a dependency, but for my project. You can assume I don't even have any dependencies. The `package.swift` I want is to describe my targets/ project, instead of using Xcode to describe the project/ package. I've come to the conclusion that Xcode configurations and package.swift are not interoperable. What a shame – Ben Butterworth Mar 26 '21 at 19:35
  • In that case just double click `Package.swift` in `Finder` (or explicitly open it in Xcode). It will load it as though it were a project all of its own. – Chip Jarred Mar 26 '21 at 19:43
  • Basically Xcode will create a `.xcodeproj` file for it within the packages folder inside a `.swiftpm` folder. You probably know that folders starting with `.` are not shown by default, but if you go to that folder in `Terminal` and type `ls -al` you'll see that it's there after you open the package in Xcode. – Chip Jarred Mar 26 '21 at 19:44
  • When you open a Swift package in Xcode like that, it does not even let you set project settings like in a normal project. You *must* do all of that in `Package.swift`. So I think it's exactly what you want. – Chip Jarred Mar 26 '21 at 19:51
  • It's not really what I want, but its the best I've got. I want the interop between Xcode and `package.swift`. When I open a non Xcode project (i.e. with `package.swift`), Xcode becomes much less useful. – Ben Butterworth Mar 26 '21 at 19:54
  • Also if you don't want Xcode's `.xcodeproj` file going into the repo, just create (or modify) `.gitignore` to include a line for `.swiftpm`. – Chip Jarred Mar 26 '21 at 19:55
  • How does Xcode become less useful when you open `package.swift` in Xcode? It's almost exactly like working in a regular project, except there are no project settings... and it in a package it always sorts the filenames. But you can do pretty much everything else you normally do in Xcode. – Chip Jarred Mar 26 '21 at 19:57
  • I guess I assumed it would be much less useful after I didn't see the blue icon for the project. Instead I see the grey wrapped package icon which doesn't have any of the xcode config settings. I'll give it a go – Ben Butterworth Mar 26 '21 at 19:59
  • Just double click on the package in Finder and play with it for a while. I think you'll find it's pretty much what you're describing you want. – Chip Jarred Mar 26 '21 at 20:01
  • I updated my answer to reflect our conversation. – Chip Jarred Mar 26 '21 at 20:08
0

I don't think this is possible. If you use a Swift package (and therefore you have a package.swift, you won't get the rich menus provided by Xcode.

I'm hoping interoperability will come in the future, like how Android Studio and Gradle config files (build.gradle) work together. Swift packages and Xcode projects do not work together at all.

Ben Butterworth
  • 6,030
  • 1
  • 28
  • 48