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)
Python's dir() and help() equivalents in Swift REPL
Swift doesn't have the methods like help()
but you can use the lldb commands.
you can input :
in Swift REPL for the shell to switch to LLDB mode.
Can I see the list of object methods in the shell (REPL)?
type lookup ClassNameOfInstance
Can I get the list of currently declared variables?
image lookup -rs lldb_expr
Can I read object reference in the shell?
expression -l swift -- instanceName
use c
command to exit LLDB mode.
addition:
When you define a variables in Swift REPL, Swift Repl make modules such as
$__lldb_expr2
,$__lldb_expr3
for each.If you would like to know lldb commands, you can use
help
command in lldb mode.
Related Topics
Swiftui: Using View Modifiers Between Different iOS Versions Without #Available
Updating a @Published Variable Based on Changes in an Observed Variable
Contextmenu on a Rounded Lineargradient Produces Sharp Edges in Swiftui
Countforfetchrequest in Swift 2.0
Nskeyedunarchiver Fails to Decode a Custom Object in Swift
Passing Arguments to #Selector Method in Swift
Is There a Technical Reason to Use Swift's Caseless Enum Instead of Real Cases
Deleting with One-To-Many Relationship
Hide Status Bar in Launch Screen
Com.Apple.Itunes Aedeterminepermissiontoautomatetarget Is Always Return -600
Swift Objc_Getassociatedobject Always Nil
Is Dispatchsemaphore a Good Replacement for Nslock
How to I Turn Off the Ambient Light in Scene Kit (With Swift)
How to Get the Unicode Codepoint Represented by an Integer in Swift
Xcode UI Test:Accessibility Query Fail on Uitableviewcell