skip to Main Content

Swift packages created by Xcode (File > New > Swift Package) show up in the root of the project or the workspace. I have a number of packages under development in my project, so after creating a few swift packages, I end up with a workspace root that looks like this

Root(Workspace)
  - SwiftPackage1
  - SwiftPackage2
  ...
  - SwiftPackage18
  - Project1
  - Project2

I would like to be able to store all my swift packages in a location that doesn’t pollute the root of the workspace, something like this:

Root(Workspace)
  - Libs  // Collapsable!
      - SwiftPackage1
      - SwiftPackage2
      ...
      - SwiftPackage18
  - Project1
  - Project2

However, after moving the package folder to another location (drag & dropping in Xcode), the folder is copied to the new location, but it’s a plain folder – and the original package is still under the root.

Is there a way to move Swift Package away from the root to keep the workspace organized?

2

Answers


  1. Chosen as BEST ANSWER

    After spending hours, I figured out a solution. The key is to create the Swift Package from the command line directly under the Libs folder.

    If you are creating a brand new Swift Package, thats' all you have to do.

    In my case I had to restore the content of all my packages using this technique. In case that's useful, these are the steps I used (Note: this is with Xcode12):

    1. I copied all my package folders to a temporary location, say ~/temp (using the filesystem/folder)
    2. In the terminal cd to the filesystem folder that matches the Lib folder under the workspace's root
    3. Repeat for every SwiftPackage1...SwiftPackage18
    mkdir SwiftPackage1
    cd SwiftPackage1
    swift package init --type library // all my packages are libraries
    
    1. Drag all the created SwiftPackageX from Finder into Xcode (same location, under Lib) so Xcode will acknowledge the resource
    2. Finally, in the filesystem (aka Finder), copy all the package folder saved in a temporary location (step #1) into the Lib folder (This will overwrite the folders created in step #3 - and that's OK)
    3. Now all the Swift Packages should show up in Xcode under Lib

    ...6 steps to just relocate a folder is pretty clunky. I would welcome any suggestions to improve!


  2. The quickest and easiest way to do this is:

    1. Move the package directory in the file system.
    2. Fix Xcode to point at the new location.

    That’s it!


    Here’s an example in more detail, of moving a Swift Package called UserInterfaces from the root of a git repository (alongside the .xcworkspace directory) into a sub-directory called Modules/.

    1. In Terminal, from the repository root directory: git mv UserInterfaces ./Modules

    2. In Xcode, select the package in the Project Navigator on the left. The package name will be red because the package is referenced from the workspace but Xcode can’t find it at the referenced location.

    The UserInterfaces package name in Xcode has gone red

    1. In the File Inspector on the right select the little folder-icon to update the referenced location of the package.

    The file inspector for the UserInterfaces package, with the select new location folder-icon circled in red

    1. Xcode will open a "Choose package location" file browser, navigate to the new location and select the package directory, then click the Choose button.

    The choose package location file browser navigated to the Modules directory with the UserInterfaces package directory highlighted with a red box and the default Choose button circled in red

    This method can be used to re-locate any files or directories in an Xcode project or workspace, and is often much quicker and easier than using the UI.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search