How to import a swift package that is using an older version of Swift tools? - github

I was trying to open a package, namely trying to import and run the swift folder under this repository.
When I Open->Package.swift file in XCode 11, it actually opened all the files under that directory. So, I thought I am one step close to running it.
But now I’m getting an error when I try to build it:
package at RepositoryName/swift’ is using Swift tools version 3.1.0 which is no longer supported; consider using ‘// swift-tools-version:5.1’ to specify the current tools version
So, I was wondering if there is a way to import and use this swift package that uses the older swift tools version?
Thanks

I solved this problem by recreating the package on XCode 11 with the same name and putting all files in the new package.
XCode provided already the Package.swift file so I didn't touch it. The first line describes the Swift tools version.
While adding it as a new package from File -> New -> Swift Package: I did set the "Add to" and "Group" to project name. And also I created the package inside this project folder.
I also added the package to target, selecting Target name -> General tab -> Frameworks, Libraries and Embedded Content -> + button to select it from the list.
Then within my project viewController I added
import [PackageNameHere]
Later on, I decided not to use it as a package, but in case anyone wants to do that this is how I did it.

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.

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.

How to import Swift Package in Playground?

Can i import Swift Package to my Playground that is in my workspace? I've already read the answer, but i think it's outdated, because now we can use Swift Packages in iOS projects.
In Xcode 12, you can use Swift Packages with Xcode Playgrounds.
Open Xcode
Create a new Workspace (File > New > Workspace...)
Add the desired Swift package via File > Add Files to "Workspace Name"... selecting the package directory.
Create a new Playground via File > New > Playground.... Ensure that you select the Workspace we just created as the "Add to" and "Group" option during Playground creation (in the assistant where you select the Playground's location on disk).
You can now import the Package target into your Playground and starting exploring the Package's functionality.
You can find additional details and a sample project in this WWDC20 session: Explore Packages and Projects with Xcode Playgrounds
Ensure to check the new "Build active scheme" checkbox in the inspector for your existing Playgrounds.
Check out Arena, it allows you to create playgrounds with SPM dependencies embedded ready for use.
All it takes is
brew install finestructure/tap/arena
arena alamofire/alamofire
Update: The Swift Package Index has since gained Arena integration and you can create a playground for a package by clicking the "Try in a Playground" button on a package page.
Still now, Playground does not support the Swift Package Manager.
When we try to add the Swift PM from Xcode using
File > Swift Packages > Add Package Dependency
the option is disabled.
An option is to use nef - it is an open-source project an lets you import third-party libraries in your Playgrounds. For now, it is compatible with Cocoapods but nef are working on open it for other dependency managers like Carthage and SPM.
About SPM is not an easy task, and it will be a bit different as Apple has prepared Xcode11 but I have good feelings and I think it could be ready for Sept-Oct.
I couldn't get #wolfrevo's answer working. However, I have a solution inspired by his:
Open Xcode
Create a New Project (File > New > Project)
Choose Muliplatform > App > Next
Project Name: Whatever > Next
Create
Now add the dependency (File > Swift Packages > Add Package Dependency)
Paste package git url: https://github.com/maxhumber/Carlo.git > Next
Select rules, or skip > Next
Finish
Create a new Playground file in this project by short-cutting Cmd+N
Search Playground > Next
Create

Adding Local dependencies in XCode11 using SPM

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.