Howto load SwiftPM project + its dependencies in Swift repl
These are the steps to follow for a solution using Swift 4.
Create a folder, let's say "TestSPMLibrary":
$ mkdir TestSPMLibrary
$ cd TestSPMLibrary
Create a library package:
$ swift package init --type library
In the "Package.swift" file, add the ".dynamic" library type.
You can also add a dependency such as Alamofire (you need to also add it to the target).
My "Package.swift" example:
// swift-tools-version:4.0
import PackageDescription
let package = Package(
name: "TestSPMLibrary",
products: [
.library(
name: "TestSPMLibrary",
type: .dynamic,
targets: ["TestSPMLibrary"]),
],
dependencies: [
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "4.0.0"),
],
targets: [
.target(
name: "TestSPMLibrary",
dependencies: ["Alamofire"]),
.testTarget(
name: "TestSPMLibraryTests",
dependencies: ["TestSPMLibrary"]),
]
)
In this library the code you want to interface with has to be declared public (and objects need a public initializer).
My "TestSPMLibrary.swift" example:
public struct Blah {
public init() {}
public var text = "Hello, World!"
}
Build the library:
$ swift build
Launch the REPL with swift -I .build/debug -L .build/debug -l
and add the library name. In my case:
$ swift -I .build/debug -L .build/debug -lTestSPMLibrary
In the REPL you can now import your library (and its dependencies):
import TestSPMLibrary
import Alamofire
let x = Blah()
print(x.text)
Starting Swift REPL for iOS vs OSX
One way to do this is to create an iOS/OS X project and set a break point to interrupt the flow. Then, enter repl
into the lldb console and enjoy.
For more on this one, watch WWDC 2014 session 409 - Introduction to LLDB and the Swift REPL.
Related Topics
How to Use .Enumerated() With Foreach in Swiftui
Ios11 Swift Silent Push (Background Fetch, Didreceiveremotenotification) Is Not Working Anymore
Propagate an Optional Through a Function (Or Init) in Swift
App Tracking Transparency Dialog Does Not Appear on iOS
Swift 2/iOS 9 - Libz.Dylib Not Found
How to Check If Time Is Within a Specific Range in Swift
Determine If Any.Type Is Optional
Sort Array by Calculated Distance in Swift
Swift Imagepickercontroller Didfinishpickingmediawithinfo Not Fired
How to Append a Tuple to an Array Object in Swift Code
How to Convert Any Generic Numeric into a Double
What Is the Use of "Static" Keyword If "Let" Keyword Used to Define Constants/Immutables in Swift
Swift: Testing Against Optional Value in Switch Case
Why Do Enums Have Computed Properties But Not Stored Properties in Swift
Highlight a Specific Part of the Text in Swiftui
How to Use an Nsattributedstring with a Scrollview in Swiftui