Adding Local dependencies in XCode11 using SPM - swift

Is it posible to integrate local dependencies with SPM in xcode 11,
File > Swift Packages > Add Package dependency...
It only seems to populate and let me select git repos hosted on bitbucket
I am just not sure if I am approaching the issue the correct way, maybe I should be using a Package.swift file instead? I have a lot of experience with Cocoapods but I would like to branch out and try other tools, especially if they are going to be supported in xcode as it seams SPM will be (Im using xcode11 beta 5)

This is the way I did it :
Drag and drop your package folder (in my example "DataStructures") from the finder directly into the Frameworks group of your target. You will see that the dropped item take a brown folder color (you can use the arrow to "get into it").
Go to your project target page, in the "Framework and Libraries" click the "+" button. Your package should show up in the "Workspace" area as a library.

Xcode 12
If you have a remote version of your package already in the project and want to work on a local. There are a few extra steps...
1. Make sure your local package's version is higher than the remote version
If you don't do that, it will continue to fetch the remote package
Select your project in Project Navigator
In the main window select Swift Packages
Select the package you want to replace with a local and remove it
Go to File > Swift Packages > Add Package Dependency...
Drag the your local package folder to the package search bar and add the prefix file://
Click on Next
Confirm that the minimum version is higher than the remote one.

Yep, Xcode makes this rudimentary task unnecessary difficult.
The trick is simple, though: in the sheet that pops up when you select 'Add Package Dependency…', in the URL field, you can enter a file:// URL. e.g. drag the package's folder into that field, which will place the path in it - e.g. '/Users/me/Documents/myShinyPackage/', then stick 'file://' to the front of it, and you'll be able to proceed.
Addendum: as some folks note in the comments below, this is problematic if you share the project with others, unless you standardise on paths. For individual use, however, it's fine.

Local Swift Package Manager(SPM)
[Dependency manager]
Key point is: file:// + git which you can use it in URL field
file://<path>
//e.g.
file:///Users/alex/Desktop/MyProject
Notes:
It works with git repository that is why please make sure that you committed your changes beforehand
when you add a SPM dependency File -> Swift Packages -> Add Package Dependency it is added into project -> Swift Packages and implicit dependency[About] is added to Frameworks, Libraries, and Embedded Content
when you committed new changes into framework do not forget File -> Swift Packages -> Update to Latest Package Versions
Product folder
<path_derived_data>/<randomizer>/Build/Products/Debug
//e.g.
/Users/alex/Library/Developer/Xcode/DerivedData/SPMExperiments-bmmqumztjedpekaajffnaegxzwzg/Build/Products/Debug
[Local CocoaPods]
[Local Carthage]

A new way(2022.11):
Simply drag the local package repo to your main project
Like this
Note: if you have that package open in another Xcode project you have to close it first.
By this way, Xcode will ignore the remote package in SPM. You can edit the local package repo directly and use it in main project.

Related

Xcode: Own Swift packages missing in Schema selector

I have some SPM swift packages as part of my swift project.
After some Xcode crashes, those packages don't appear in the Schema selector (black arrow in image) any more.
So I can't select and build the swift packages any more separately.
What can I do to fix this?
For me it was solved by deleting .swiftpm/ folder
(Although, maybe just removing xcshareddata would've been enough)
Steps:
Removing dependency + reference from the main project
Delete .swiftpm/ of "problematic" local package
Add the package dependency back to the project
At least it worked.. Hoping there's a better way to solve this as well.
There must be a better way to resolve this, but at least it works:
Usual housekeeping:
Clear derived data
File -> Packages -> Reset Package Caches
restart Xcode
For each package that Xcode doesn't find / build:
Klick on package -> show in Finder
in finder, move the whole package into a temporary folder (package in Xcode gets red)
delete red package in Xcode
Create package again in Xcode with the same name (File -> New -> Package...), add package to appropriate targets again
remove the newly generated swift files in XCode, including Package.swift
drag the files in your copied folder into the corresponding place in the Xcode package
File -> Packages -> Reset Package Caches (for some reason Xcode loses external packages each iteration)
Build -> should be one less missing package in the error messages

Editing a Package Dependency as a Local Package

Overview
I have a project that uses a remote swift package.
I would like to edit this package in the same project so that I can refine it based on the usage.
Aim:
To do this I am trying to add the same package as a local package so that I can edit it.
Reference:
https://developer.apple.com/documentation/swift_packages/editing_a_package_dependency_as_a_local_package
Problem:
When I drag and drop the cloned folder containing Package.swift into the project, I can't see any of the local package source files in the project. (see screenshot)
I have been breaking my head over it. Any help on this would be much appreciated.
Xcode Version:
13.2 (13C90) (downloaded from the developer portal not the AppStore)
The reason you cannot see the content of the package is that you have it open in another Xcode instance.
You can only edit a package in one Xcode instance at a time.
I find closing down Xcode completely, then opening up the particular project that I want to edit the package in solves the problem.

Xcode 13.1 bug editing dependency as local

Just upgraded to Xcode 13.1 and opened a SPM project by clicking the Package.swift file that has some dependencies.
In the former versions I could simply edit one of these dependencies by dragging a local clone of the package into the Project Navigator.
Xcode was then going to ignore the checked out dependency and let me edit the local one.
Apple documents this here.
When trying this with Xcode 13.1, Xcode just copies the folder into the project folder, like it does with normal files/folders that are not SPM projects.
Did I find a bug or missed I something?
Thanks!
Update: I found out how to work around by creating a .xcodeproj file with swift package generate-xcodeproj. There I could drag in the package as always. This works but is not the way to go, as the xcodeproj generation is deprecated by apple.
It looks like there is now an Add Local... button at the bottom of the add package wizard. Selecting the package from your local filesystem yields the same result as the "drag from finder" instructions you've linked.
The wizard is accessible from:
File > Add Packages, or
Project.xcodeproj > Package Dependencies > +.
I was having this problem, too, but after seeing the first comment on the other answer, I tried this:
I used File->Add Packages->Add Local…->navigate to local clone of dependency->Copy Package, which indeed seems to do nothing, but it puts a .package dependency line onto the clipboard.
I then edited my main project’s Package.swift to replace the existing Github dependency with the contents of the clipboard. Now I can edit the dependency’s files directly in the “Package Dependencies” portion of the project contents pane (which I wish you could do for a regular Xcode project too).
This solution kinda sucks, because it forces me to modify Package.swift, which means everyone on the team has to set things up the same way. Fortunately, I'm the only one working on this project, but future me is sure to be bitten by this.

How to remove a Swift Package Dependency from the Xcode source control navigator

I tried out a package but decided not to use it. So I deleted the according dependency via Project Settings -> Swift Packagesm which worked so far. The point is, that the according repository is still listed in the Source Control Navigator - below my own project and with a group called "Swift Package Dependencies". How to remove that repository? When I right-click on it the delete button is disabled. When I search the project for the according package I don't find anything...
I found a solution: The file /Users/<USER>/Library/Developer/Xcode/DerivedData/<APPNAME>/Blueprint.xcscmblueprint still contained a reference to the package remote. I deleted that file and now the remote repository of the package is no longer shown in he source control navigator!

How to update swift package imported to Xcode Project via Swift Package Manager?

I know how to create new Swift Package using Xcode
I now how to add in Project Settings new Swift Package using Swift Package Manager tab
But how I can refresh it i.e. force to download updated version from github.
Currently I do not have tags for versioning so I choose to use master branch
What I do currently is remove dependency and add it again in Swift Package Manager.
Option 1: (Xcode 13 and above)
Right-click on the package from the left navigation pan and select Update Package
Option 2:
Double click on the package in the tab you mentioned and change the version to anything else. It will then recheck the remote repo. The benefit of doing this is to only update the selected package. (Also, it's better to have the current using version be set in the package.)
Option 3:
From File -> Swift Packages -> Update to Latest Package Versions
If it is not added with version, i.e. through branch name, then follow the steps.
1. Clear derived data
2. Go to Xcode file-> Swift Packages -> Update to Latest Package Versions
If your remote repo is on vpn, dont forget to enable it and then update the package.
You can see the package getting checked out and added to the target.