Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I import my project code into the Swift REPL?

The Swift REPL is great, but it would be even better if I could import the classes from an Xcode project. I tried switching to my project directory and running

$ swift
> import ProjectName

but I got:

error: no such module 'ProjectName'

Is it possible to do this?

like image 502
Bill Avatar asked Nov 12 '14 17:11

Bill


People also ask

Does REPL IT support Swift?

The Swift distribution comes with a REPL for the Swift language. The Swift REPL is a great tool for experimenting with Swift code without needing to create a throwaway Swift package or Xcode project. The REPL can be launched by running the swift command without any arguments.

How does Swift REPL work?

The Swift REPL allows you to import the core libraries like Foundation , Dispatch and system modules like Darwin on macOS and Glibc on Linux. In fact, the REPL allows you to import any Swift module as long as it can correctly find and load them using the compiler arguments that are provided while launching the REPL.


1 Answers

The Swift REPL includes a number of different options. Use swift -help to see them. For your case, if you've defined ProjectName as a framework target and in the target you've declared 'Defines Module' then you can access it with:

$ swift -F <install path with subdirectory ProjectName.framework>
> import ProjectName

Here is an example:

$ swift -F /Users/.../Library/Developer/Xcode/DerivedData/Opus-bsjennhdtvmqrhejuabovdyxlqte/Build/Products/Debug/
Welcome to Swift!  Type :help for assistance.
  1> import OpusOSX
  2> version                    // var from framework
$R0: String = "Opus 1.0"
  3> any([1,3]) { 0 == $0 % 2 } // 'any()' in framework
$R1: Bool = false
  4> any([1,2,3]) { 0 == $0 % 2 } 
$R2: Bool = true
  4> any([1,2,3,4], conjoin ({ 0 == $0 % 2 }, { $0 >= 3 })) 
$R3: Bool = true
like image 152
GoZoner Avatar answered Sep 27 '22 21:09

GoZoner