How to use Swift Package Manager in Xcode 9 Playground [duplicate] - swift

This question already has answers here:
How to import Swift Package in Playground?
(4 answers)
Closed 7 months ago.
Is it possible to use Swift Package Manager inside Xcode 9 Playground?

It is not possible to use the Swift Package Manager in a playground at this time. The reason for that is that the Swift Package Manager is primarily used outside of apple platforms.
Apple mentions this on the Swift Package Manager's GitHub:
Note that at this time the Package Manager has no support for iOS, watchOS, or tvOS platforms
The Swift Package Manager is especially useful for writing Swift for platforms such as Linux and Mac. Uses may include creating servers based written in Swift or writing programs that can be run without a user interface (think home automations and IoT). It is a great way to organize packages without using Pods or other third-party setups. When you build the code for production (or debugging) it will download and incorporate packages into the project.
Note: This may change in the future, but is current as of Swift 3

In Xcode 12, you can use Swift Packages with Xcode Playgrounds.
Steps to setup a workspace with a Swift Package and a Playground:
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.

In Xcode 12, playgrounds can work seamlessly with swift packages in a project.
Please follow the steps below:
Create a project named Playground:
File → New → Project... (⇧⌘N) → Product Name: Playground
⚠️ You can name it whatever you want, here is just an example.
Add a swift package for the project, such as SnapKit:
File → Swift Packages → Add Package dependency... → https://github.com/SnapKit/SnapKit.git
Create a playground and add it to the project created in the first step:
File → New → Playground... (⌥⇧⌘N) → Add to: Playground
Import the swift package (SnapKit) in the Playground:
import SnapKit
If nothing else, you can use SnapKit in the Playground.

Related

How to set custom working directory in Xcode for Swift packages?

So I was trying to set "custom working directory" in Xcode for my Swift package, like this
You can see I used the good old $(PROJECT_DIR) here.
But when I started to run the project, Xcode prompted me with this
So how can I make this work?

Swift, Xcode: Using Swift FrameWork in current Project with pods

I have a tvOS project developed in Swift 3.2 + Xcode 8.3.3, and Objective-C.
When I started developing the project, I started with SingleViewApplication and later added PODS which generated ABC.xcworkspace for me.
Now I open ABC.xcworkspace with Xcode for any development.
The project hierarchy is:
-ABC
--ABC folder
-Pods
--Podfile
After I create a framework in Swift and add it to the project by Files -> Add Files to ABC and then add it to Embedded binaries And Linked Frameworks and Libraries
Then, I tried to import and use UIViewController created in Framework.
I see the error no such module.
I referred to other posts for resolving this error such as setting path recursive in framework search path, but nothing is working for me.
If you need more information, please let me know.
Know that, if I follow the standard way ie 1. creating new project 2. create new framework 3. create new xcworkspace then drag 1 & 2 in 3, it works fine.

How do I use Swift Package Manager with an existing macOS project?

I've been using Cocoapods to manage dependencies for my Swift project. I came across this package which is not listed at Cocoapods. Instead it suggests using Swift Package Manager. However, whenever I try to use Swift Package Manager to do basically anything, it ends up completely destroying my entire project.
So, in order to figure out how to actually use Swift Package Manager, I'm playing with it in a test project.
Here's what I've tried:
Create a new project from Xcode
File -> New -> Project -> Cocoa App
Product Name: basic-ssh-test
This creates a basic application which loads a blank window when I hit "Run". Just for fun I added print("test") to the applicationDidFinishLaunching function in my AppDelegate so the debug window says "test" when I run the program.
Now I want to add the Shout package as a dependency.
swift package init --type executable
This creates the following files:
Creating executable package: basic-ssh-test
Creating Package.swift
Creating README.md
Creating .gitignore
Creating Sources/
Creating Sources/basic-ssh-test/main.swift
Creating Tests/
Now I'm going to add the "Shout" dependency to my new Package.swift file:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "basic-ssh-test",
dependencies: [
.package(url: "https://github.com/jakeheis/Shout", from: "0.2.0")
],
targets: [
.target(
name: "basic-ssh-test",
dependencies: ["Shout"]),
]
)
And I'll pull in the dependencies:
swift package resolve
This results in the dependencies being pulled into the .build directory:
Fetching https://github.com/jakeheis/Shout
Fetching https://github.com/jakeheis/CSSH
Fetching https://github.com/IBM-Swift/BlueSocket
Cloning https://github.com/IBM-Swift/BlueSocket
Resolving https://github.com/IBM-Swift/BlueSocket at 0.12.91
Cloning https://github.com/jakeheis/CSSH
Resolving https://github.com/jakeheis/CSSH at 1.0.3
Cloning https://github.com/jakeheis/Shout
Resolving https://github.com/jakeheis/Shout at 0.3.0
Now I regenerate the xcodeproj file:
swift package generate-xcodeproj
Now when I open the xcodeproj file there is a new group called Sources that has a single directory basic-ssh-test with a single swift file in it main.swift with print("Hello, world!").
The code that I'm actually interested in running is now in a blue folder called basic-ssh-test. All of the necessary files are still in there, but instead of running my application, Xcode is running the main.swift file. I can tell because the debug output is "Hello, world!" instead of "test".
I've read a couple of tutorials that claim that Swift Package Manager will move my source files and continue to build the same as before, but that's clearly not the case.
There's also no longer a "Main Interface" option in my Build Settings, so I can't select "MainMenu.xib" as my application starting point.
This is essentially the same thing that happens when I try to use Swift Project Manager with my existing project. It pulls in the dependencies, but it basically ignores my entire existing project and just runs the "hello world" code.
How do I use Swift Package Manager to add a dependency to an existing project without ignoring the existing codebase?
I think SPM is only compatible with Mac command line executables or libraries. This explicitly states SPM doesn't support iOS, watchOS, or tvOS platforms at all. But, since macOS AppKit/Cocoa application targets are very similar to iOS or tvOS Xcode targets, I would say this statement implies that SPM can't be used with macOS Cocoa applications out of the box either, which is what I think you're hoping for.
It looks like there is some work here on how to use it with iOS application targets which should largely translate to a macOS Cocoa target.
Until the time of this writing answer, Swift Package Manager do not support the iOS, tvOS, and watchOS. Instead, you will have to add the files from the package directly to your project.
I would suggest you creating a Dependencies group in your project and a group below that with the package name, like this answer:
So, first you will add the files dependency that you want to import into your project to the name of the package name that included in Dependencies group that we make before.
After you add the files then you can access the code as you usually would when you write it yourself like the image below. No need imports. You can see more details in here. hope it helps.
You can create a windowed application using SPM (I have a whole project sitting waiting for ABI that does this) you just need some boiler plate in swift.main. I’ll dig it out.
However the method others are suggesting is never going to work (with current versions of SPM).
However, what I am seeing is that you really want to use SPM to manage dependencies (keeping up to date etc). So I think you could do that much more easily.
Rather than creating a executable as your default target, instead create a library (perhaps call it basic-ssh-dependencies). Generate your Xcode project and drag THAT into your primary Xcode project and configure your target to to depend on it. When you update the library from SPM for changes in dependencies you should re-gen your Xcode-proj.
Let me know your mileage and any wrinkles, or if I’ve misunderstood what you are trying to achieve.
Update for XCode 11
This is A LOT easier now with XCode 11. As of this writing the current version of XCode is 11.3.1.
For an existing XCode project simply:
go to File->Swift Packages->Add Package Dependency...
enter the URL of the hosted package you want to add
choose the rules about which versions your app will use
click Finish

how to correctly add sqlite frameworks to the Xcode project?

I am trying to add SQLite to my project.I checked the target dependencies under the build phase tab,it is empty, which is true.I am getting the following error:Unable to run command 'Ld SQLite' - this target might include its own product."
I am using swift 3.
Can you please help me? Thanks in advance
I'm not in front of a Mac at present, but I think all you need to do under Build Phases is go to Link Binaries with Libraries, hit the add button and type in "sqlite" so you can select libsqlite3.
Xcode 9 and Swift 3.2 (or, obviously Swift 4), just import SQLite3 in your relevant Swift file ...
import SQLite3
... and it will import the library for you.
In older Xcode versions (or if you turned off automatic library/framework linking), you have to manually link to the libsqlite3 library in the target settings under the “Build Phases”:
But in Xcode 9 with the default settings, all you need is that import line.

swift package manager outputs not shown in Xcode

I have started a project with swift package manager:
swift package init --type executable
then i made it an xcode project
swift package generate-xcodeproj
Now i am trying to build it from Xcode, it says build-succeeded but i can't see any result at output:
For example, should not this code puts something to output:
This is a bug: generate-xcodeproj generates two conflicting schemes.
Go to "Manage Schemes" and delete one of the duplicates.
Then "Edit" the remaining one: in its "Run" tab, "Executable" may be set to "None", in this case you have to change it to your executable target (it will be listed in the popup).
Build again and you're good to go.