我可以将我的项目代码导入Swift REPL吗?

时间:2023-01-23 22:10:17

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 REPL非常棒,但是如果我可以从Xcode项目导入类就更好了。我尝试切换到我的项目目录并运行

$ swift
> import ProjectName

but I got:

但我有:

error: no such module 'ProjectName'

Is it possible to do this?

有可能这样做吗?

1 个解决方案

#1


13  

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 REPL包含许多不同的选项。使用快速帮助来查看它们。对于您的情况,如果您已经将ProjectName定义为框架目标,并且在目标中您声明了“定义模块”,那么您可以通过以下方式访问:

$ 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

#1


13  

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 REPL包含许多不同的选项。使用快速帮助来查看它们。对于您的情况,如果您已经将ProjectName定义为框架目标,并且在目标中您声明了“定义模块”,那么您可以通过以下方式访问:

$ 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