How to import Swift Package in Playground? - swift

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

Related

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 import a swift package that is using an older version of Swift tools?

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.

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.

Importing a Swift project from Linux to Mac

I used CLion and its Swift plugin to start work on a little Swift project. I uploaded the current status to github and now want to work on that on my mac (I followed this tutorial: https://blog.jetbrains.com/clion/2015/12/swift-plugin-for-clion/). How do i import that project on mac now, though? I have AppCode and XCode installed but cant really get it to work. I used AppCode's "import from github" feature but that leaves me with a project that does show the CMakeLists.txt and Package.swift, but not my Sources/ folder at all. And in XCode i can only start new projects for applications, not a static command line lib without any GUI like this project is supposed to be.
So: How do i import them on mac in AppCode (preferred!) or XCode?
Thanks!
EDIT: Oh an when trying to simply checkout my github repo in AppCode it states "no project file found".
It sounds like you've created a SwiftPM package, since you have a Sources directory and a Package.swift file. If that is correct, you can create an Xcode project with the following command:
swift package generate-xcodeproj
AppCode needs xcodeproj to open the project, without it you cannot open it in AppCode. But you can use the CLion on Mac.

How to use frameworks imported with carthage in swift playground

i have a swift project with some frameworks added via carthage. Is it possible to use those frameworks in playground inside project and how to use it, because
import Argo
doesn't work :(
This stopped working at some point. Sigh
What I do now is
Create a macOS > Command Line Tool.
Create a Cartfile with github "ReactiveX/RxSwift" and run carthage update --platform iOS
Go to the command line tool target and add the frameworks from Carthage/Build/iOS in Linked frameworks and Libraries
Add the playground files.
At this point I’m able to run the playground files.
🤷🏻‍♂️
A playground has access to external frameworks if it is part of a workspace that builds a target configured to access those frameworks.
If you want to add a playground to an existing carthage project, you only need to save the project as a workspace (File > Save as Workspace…), build the target, and you are done.
If you just want to distribute a playground with third party frameworks, you need to create a dummy workspace. Here is a step by step example for a playground with the RxSwift framework:
Create a new Xcode project of type Cross-platform > Other > Empty. Name it RxPlayground. This will create this structure RxPlayground/RxPlayground.xcodeproj and open a blank Xcode.
Download RxSwift with Carthage
Create a Cartfile with this line: github "ReactiveX/RxSwift" "swift4.0"
Run Carthage with carthage update --platform iOS.
Add a playground to the project.
Click File > New > Playground…
Choose the iOS > Blank template and name it Rx.playground
Right click the project node and choose “Add Files to RxPlayground”.
Select the Rx.playground and add it.
Create a workspace
Click File > Save as Workspace…
Save as Rx.xcworkspace
Copy the frameworks to the products directory.
Close the project and open the Rx.xcworkspace
Create a Cross-platform > Other > Aggregate. Name it RxAggregate
Create a New Run Script Phase with the following content:
cp -rv "${SRCROOT}/Carthage/Build/iOS/" "${BUILT_PRODUCTS_DIR}/${FRAMEWORKS_FOLDER_PATH}"
At this point, Xcode and the Finder look like this:
Note that Carthage/ and Cartfile.resolved appear when you run Carthage, Without them, your playground will be only a few Ks.
Lastly, build the project (⌘B). Now you can use the framework in your playground:
//: Playground - noun: a place where people can play
import RxSwift
_ = Observable<Void>.empty()
.subscribe(onCompleted: {
print("Completed")
})
Sometimes the first time you build (⌘B) Xcode doesn’t notice the new framework (sigh). What I do is click on the target and back to a source file, or re-open the project. I don’t have an explanation why this happens.
In order for the framework to work within a playground, the project that produces the framework must be included in the workspace for your project. So to make this work you need to follow these steps:
If your project is not inside a workspace, create a workspace for your project by choosing File > Save As Workspace in Xcode.
Drag the .xcodeproj file from the Carthage/Checkouts folder into your workspace.
Run the build action on your framework target.
I solved it by copying the built frameworks to the Built Products Directory, where Playgrounds in the workspace also searches for frameworks.
Note: you also need to run lipo and strip away unused architectures from the FAT binaries.
See more here:
https://github.com/richardnees/CarthagePlaygrounds
Based #Jano's great answer (thanks for that), I created a fully functional playground to interact with Carthage frameworks:
https://github.com/backslash-f/carthage-playground
I included the Charts framework as an example:
I tested with latest Xcode.
"Works on my machine." 👍🏻
Found how to do this without converting to workspace and adding .xcodeproj file from the Carthage/Checkouts folder into your project.
This works if you have some framework target that contains all sources you want to use in playground. This framework must be in "Embedded frameworks" of your main target.
Add Carthage .framework to your project from Carthage/Build/IOS
Drag and drop it from your project tree into "Embedded
frameworks" for your main target
Add Carthage .framework to your
framework target as Optional(In target Membership)
Add import
"Your framework target" into playground
That worked for me