我读了Xcode 6中的新功能.本文介绍了一些关于Xcode 6的新功能,它说:
命令行
Xcode的调试器包含Swift语言的交互式版本,称为REPL(读取 - 评估 - 打印 - 循环).使用Swift语法评估正在运行的应用程序并与之交互,或者在类似脚本的环境中编写新代码.REPL可以从LLDB内部的Xcode控制台或终端获得.
我想知道如何获得REPL?
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
然后你可以做其中一个:
xcrun swift lldb --repl
从Xcode 6.1开始 - swift
在终端中输入也会启动REPL.
或者,如果您不想搞乱当前的开发环境,可以运行:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift
与从终端运行Swift的方式相同,您也可以执行脚本.只需使用以下shebang,然后运行您的脚本.(根据 Swift的创建者Chris Lattner)
#!/usr/bin/env xcrun swift -i
如果有人关心一个简单的Swift脚本shebang:
#!/usr/bin/env xcrun --sdk macosx swift
如果需要特定的目标版本
#!/usr/bin/env xcrun --sdk macosx swift -target x86_64-macosx10.11
如果需要特定的工具链(比如你想使用Swift 2.3,但你使用的是Xcode 8)
#!/usr/bin/env xcrun --toolchain com.apple.dt.toolchain.Swift_2_3 --sdk macosx swift -target x86_64-macosx10.11
如果你想在你的Xcode 7.3.1中使用Swift 2.2,我们假设Xcode 7.3.1位于 /Applications/Xcode7.app
sudo xcode-select -s /Applications/Xcode7.app/ xcrun --sdk macosx swift
从现在开始,默认的活动开发者目录已更改,您可以使用以下命令检查:
xcode-select -p
如果您想使用Swift.org提供的快照,请不要错过这里的安装.
作为构建阶段,我首先在Xcode iOS项目的Run swift脚本中回答
在安装了命令行工具的Xcode 6.1.1中,您可以通过/usr/bin/swift
以下方式直接引用来执行脚本:
#!/usr/bin/swift let variable: String = "string" print("Test \(variable)")
步骤1:打开终端
步骤2:输入"swift"
步骤3:没有第3步
例:
GoldCoast:~ macmark$ swift Welcome to Swift! Type :help for assistance. 1> println("Hello, world") Hello, world 2> var myVariable = 42 myVariable: Int = 42 3> myVariable = 50 4> let myConstant = 42 myConstant: Int = 42 5> println(myVariable) 50 6> let label = "The width is " label: String = "The width is " 7> let width = 94 width: Int = 94 8> let widthLabel = label + String(width) widthLabel: String = "The width is 94" 9> :exit GoldCoast:~ macmark$
**自xcode6 beta 4起更新**
这也可以在xcode首选项上完成.只需转到xcode - > preferences - > locations.
对于命令行工具,只需从下拉列表选项中选择所需的版本,请参阅下面的图片.(swift要求路径为xcode6的路径).
我也将在下面留下我之前的答案.
Kaan说了什么,你也可以使用苹果脚本来制作简单的应用程序,这样你就可以用它来回切换.
打开苹果脚本>粘贴下面的代码并将其导出为应用程序,只需单击一下即可切换到默认路径或beta路径(使用swift)
set xcode6Path to "xcode-select -switch /Applications/Xcode6-Beta.app/Contents/Developer" set xcodeDefaultPath to "xcode-select -switch /Applications/Xcode.app/Contents/Developer" display dialog "set xcode sdk path to " buttons {"xcode 6", "default"} default button 1 copy result as list to {buttonPressed} if buttonPressed is "default" then try do shell script xcodeDefaultPath with administrator privileges end try else try do shell script xcode6Path with administrator privileges end try end if
然后运行> xcrun swift
放弃
该脚本假设您同时安装了xcode6-beta和xcode5.
如果你是一个只尝试xcode6beta 的新开发人员,你将不需要手动任何脚本或设置路径.只需运行xcrun swift
路径已经为您设置.
当xcode6最终发布时,您需要将此路径重置为此简单应用程序的默认路径,并且永远不要再使用它.
xcrun命令将使用DEVELOPER_DIR环境变量来覆盖当前选定的Xcode安装(使用xcode-select设置).您可以使用它来构造一个单独的命令,该命令将在命令行上运行swift并将您置于REPL中.看起来像这样:
/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift
你可以将其别名为'swift':
alias swift="/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift"
作为一个有趣的附注,您可以使用相同类型的调用来运行swift脚本,就像您使用bash或python一样添加-i:
#!/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift -i println("Hello World!")
当然,一旦正式发布Xcode 6并将其切换为默认开发人员工具,您可以删除DEVELOPER_DIR = ..位并使用"xcrun swift".
安装官方Xcode 6.1版本后,有一个swift
命令/usr/bin/swift
.
请记住,如果您在路径中使用的Python与Apple提供的Python不同,则swift
可能会失败ImportError: No module named site
.在这种情况下,请确保export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin
在致电之前执行此操作swift
.