13

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?

Bill
  • 44,502
  • 24
  • 122
  • 213

1 Answers1

16

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
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • 1
    Can this work for an iOS app project, rather than a framework? – Bill Jan 13 '15 at 03:16
  • 1
    Probably not, for two reasons. First, the `swift` program doesn't have any command line options for importing projects and second, the `swift` program is not a cross-compiler and thus only works for MacOSX. Note: you get a 'cross-compiler' using a IOS Playground in Xcode just not with the command line via `Swift`. – GoZoner Jan 13 '15 at 14:37
  • 1
    to use the REPL for iOS see: http://stackoverflow.com/questions/24385734/starting-swift-repl-for-ios-vs-osx/24388630#24388630 – Ultrasaurus Nov 25 '15 at 04:04
  • For standalone iOS swift repl with autocompletion and history navigation: http://stackoverflow.com/a/43478888/3195266. – Netsu Apr 19 '17 at 09:39