我如何在终端使用swift ?

时间:2023-01-22 16:27:38

I read What's new in Xcode 6. The article introduces some new feature about Xcode 6, and it says:

我读了Xcode 6中的新内容。本文介绍了Xcode 6的一些新特性,并指出:

Command Line

命令行

Xcode’s debugger includes an interactive version of the Swift language, known as the REPL (Read-Eval-Print-Loop). Use Swift syntax to evaluate and interact with your running app or write new code in a script-like environment. The REPL is available from within LLDB in Xcode’s console, or from Terminal.

Xcode的调试器包括Swift语言的交互式版本,即REPL (Read-Eval-Print-Loop)。使用Swift语法对正在运行的应用程序进行评估和交互,或者在类似脚本的环境中编写新代码。REPL可以从Xcode的控制台的LLDB内部或终端中获得。

I want to know how to get the REPL?

我想知道怎样才能拿到退款?

12 个解决方案

#1


130  

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

then you can do one of these:

然后你可以这样做:

xcrun swift 
lldb --repl

As of Xcode 6.1 - typing swift in the terminal launches the REPL as well.

从Xcode 6.1开始——在终端输入swift也会启动REPL。

#2


58  

Alternatively, if you don't want to mess up your current dev environment, you can just run:

或者,如果您不想破坏当前的开发环境,您可以运行:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift

#3


37  

Step 1: Open Terminal
Step 2: Type "swift"
Step 3: There's no step 3

步骤1:打开终端2:输入“swift”步骤3:没有步骤3

Example:

例子:

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$ 

#4


15  

In the same fashion as running Swift from the Terminal, you can also execute scripts. Just use the following shebang, and run your script. (As per Chris Lattner, creator of Swift)

与从终端运行Swift一样,您也可以执行脚本。只需使用以下shebang,并运行脚本。(正如斯威夫特的创造者克里斯·拉特纳所说)

#!/usr/bin/env xcrun swift -i

#5


14  

In Xcode 6.1.1 with Command Line Tools installed you can execute scripts by referencing directly to /usr/bin/swift the following way:

在Xcode 6.1.1中,安装了命令行工具,您可以通过以下方式直接引用/usr/bin/swift来执行脚本:

#!/usr/bin/swift

let variable: String = "string"
print("Test \(variable)")

#6


9  

If any one cares a simple Swift script shebang:

如果有人关心一个简单的Swift脚本,她说:

#!/usr/bin/env xcrun --sdk macosx swift

If specific target version is required

如果需要特定的目标版本

#!/usr/bin/env xcrun --sdk macosx swift -target x86_64-macosx10.11

If specific toolchain is required (like you want to use Swift 2.3 but you are using Xcode 8)

如果需要特定的工具链(如您希望使用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

If you want to use Swift 2.2 in your Xcode 7.3.1, let's assume Xcode 7.3.1 is located at /Applications/Xcode7.app

如果您想在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

from now on the default active developer directory changed, you can check that using:

从现在开始,在默认的活动开发人员目录上修改后,您可以使用:

xcode-select -p

If you want to use snapshots provided by Swift.org, you should not miss Installation here.

如果您想使用Swift.org提供的快照,请不要错过这里的安装。


as first answered by me in Run swift script from Xcode iOS project as build phase

正如我在Xcode iOS项目运行swift脚本作为构建阶段的第一次回答

#7


8  

** update as of xcode6 beta 4 **

** *更新xcode6 beta 4 **

this can also be done on xcode preferences. simply go to xcode -> preferences -> locations.

这也可以在xcode首选项上完成。只需转到xcode ->首选项->位置。

for command line tools simply select the version you want from drop down list options, refer picture below. (swift requires path to be xcode6's path).

对于命令行工具,只需从下拉列表选项中选择您想要的版本,请参考下面的图。(swift要求路径为xcode6的路径)。

我如何在终端使用swift ?

I will leave my previous answer below as well.

我也将把之前的答案留在下面。


what Kaan said and you can also use an apple script to make simple application so you can use it to switch back and forth.

Kaan说过,你也可以用苹果的脚本制作简单的应用程序,这样你就可以用它来回切换。

open apple script > paste this below code & export it as an application so with just one click you can switch to default path or beta path (to use swift)

打开苹果脚本>粘贴如下代码并将其导出为应用程序,只需单击一次即可切换到默认路径或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

then run > xcrun swift

然后运行> xcrun swift

disclaimer

免责声明

  1. the script assumes you have both xcode6-beta & xcode5 installed.
  2. 脚本假定您已经安装了xcode6-beta和xcode5。
  3. if you're a new developer who's trying out only xcode6beta you will not need any script or setting path manually. simply run xcrun swift as the path is already set for you.
  4. 如果您是一个只尝试xcode6beta的新开发人员,您将不需要任何脚本或手动设置路径。只需运行xcrun swift,因为路径已经为您设置。
  5. when xcode6 is finally released you will need to reset your path back to default from this simple app and never use it again.
  6. 当xcode6最终发布时,您将需要从这个简单的应用程序将路径重置为default,并且永远不再使用它。

#8


6  

After installing the official Xcode 6.1 release, there is a swift command in /usr/bin/swift.

在安装正式的Xcode 6.1版本之后,在/usr/bin/swift中有一个swift命令

Bear in mind that if you have a Python different from the Apple-supplied Python in the path, swift can fail with ImportError: No module named site. In that case, make sure that you do export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin before calling swift.

请记住,如果您有一条与路径上的苹果提供的Python不同的Python,那么swift可能会因为impor恐怖而失败:没有一个名为site的模块。在这种情况下,请确保在调用swift之前,您要执行export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin。

#9


5  

The xcrun command will use the DEVELOPER_DIR environment variable to override the currently selected Xcode installation (as set with xcode-select). You can use that to construct a single command that'll run swift on the command line and put you in the REPL. That looks like this:

xcrun命令将使用DEVELOPER_DIR环境变量覆盖当前选定的Xcode安装(如使用Xcode -select设置的那样)。您可以使用它构造一个命令,在命令行上运行swift并将您放入REPL中。像这样:

/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift 

and you can alias that to just 'swift':

你可以将其别名为“swift”

alias swift="/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift"

As an interesting side note, you can use the same kind of invocation to run a swift script just like you'd use bash or python by adding a -i:

有趣的是,您可以使用相同的调用来运行一个swift脚本,就像您使用bash或python时添加-i:

#!/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift -i

println("Hello World!")

Of course, once Xcode 6 is released officially and you switch to that as your default developer tools, you can drop the DEVELOPER_DIR=.. bits and just use "xcrun swift".

当然,一旦Xcode 6正式发布,并将其转换为默认的开发工具,就可以删除DEVELOPER_DIR=..使用“xcrun swift”即可。

#10


4  

make sure you install xcode 6.0 ,but not 6.1

确保您安装的是xcode 6.0,而不是6.1

If you get an error:

如果你犯了错误:

<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift

just run

你就跑

xcrun --sdk iphonesimulator8.0 swift

or you can

或者你可以

export SDKROOT="iphonesimulator8.0" 

and then

然后

xcrun swift

Use "xcodebuild -showsdks" to list the available SDK names.

使用“xcodebuild -showsdks”列出可用的SDK名称。

if you install xcode 6.1,just

如果您安装了xcode 6.1

sudo xcode-select -s /Applications/*your-Xcode-6.1-path.app*/Contents/Developer
xcrun swift

#11


2  

For XCode6, run these commands:

对于XCode6,运行以下命令:

$ sudo xcode-select -s /Applications/Xcode.app/Contents/Developer/

$ xcrun swift

If you get an error:

如果你犯了错误:

<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift

try:

试一试:

xcrun swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk

#12


1  

open Terminal,

打开终端,

$sudo xcode-select -switch /Applications/Xcode6-Beta6.app/Contents/Developer

$ sudo xcode-select开关/应用程序/ Xcode6-Beta6.app /内容/开发人员

Notice: The Xcode6-Beta6.app should be replaced to appropriate version you installed

注意:Xcode6-Beta6。应用程序应该被替换为您安装的适当版本

Then put this line alias swift='xcrun swift' to ~/.bash_profile

然后将这行别名swift='xcrun swift'放到~/.bash_profile中

And,

而且,

$source ~/.bash_profile

$源~ / . bash_profile

$swift

美元迅速

There you go!

你走吧!

#1


130  

sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer

then you can do one of these:

然后你可以这样做:

xcrun swift 
lldb --repl

As of Xcode 6.1 - typing swift in the terminal launches the REPL as well.

从Xcode 6.1开始——在终端输入swift也会启动REPL。

#2


58  

Alternatively, if you don't want to mess up your current dev environment, you can just run:

或者,如果您不想破坏当前的开发环境,您可以运行:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift

#3


37  

Step 1: Open Terminal
Step 2: Type "swift"
Step 3: There's no step 3

步骤1:打开终端2:输入“swift”步骤3:没有步骤3

Example:

例子:

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$ 

#4


15  

In the same fashion as running Swift from the Terminal, you can also execute scripts. Just use the following shebang, and run your script. (As per Chris Lattner, creator of Swift)

与从终端运行Swift一样,您也可以执行脚本。只需使用以下shebang,并运行脚本。(正如斯威夫特的创造者克里斯·拉特纳所说)

#!/usr/bin/env xcrun swift -i

#5


14  

In Xcode 6.1.1 with Command Line Tools installed you can execute scripts by referencing directly to /usr/bin/swift the following way:

在Xcode 6.1.1中,安装了命令行工具,您可以通过以下方式直接引用/usr/bin/swift来执行脚本:

#!/usr/bin/swift

let variable: String = "string"
print("Test \(variable)")

#6


9  

If any one cares a simple Swift script shebang:

如果有人关心一个简单的Swift脚本,她说:

#!/usr/bin/env xcrun --sdk macosx swift

If specific target version is required

如果需要特定的目标版本

#!/usr/bin/env xcrun --sdk macosx swift -target x86_64-macosx10.11

If specific toolchain is required (like you want to use Swift 2.3 but you are using Xcode 8)

如果需要特定的工具链(如您希望使用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

If you want to use Swift 2.2 in your Xcode 7.3.1, let's assume Xcode 7.3.1 is located at /Applications/Xcode7.app

如果您想在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

from now on the default active developer directory changed, you can check that using:

从现在开始,在默认的活动开发人员目录上修改后,您可以使用:

xcode-select -p

If you want to use snapshots provided by Swift.org, you should not miss Installation here.

如果您想使用Swift.org提供的快照,请不要错过这里的安装。


as first answered by me in Run swift script from Xcode iOS project as build phase

正如我在Xcode iOS项目运行swift脚本作为构建阶段的第一次回答

#7


8  

** update as of xcode6 beta 4 **

** *更新xcode6 beta 4 **

this can also be done on xcode preferences. simply go to xcode -> preferences -> locations.

这也可以在xcode首选项上完成。只需转到xcode ->首选项->位置。

for command line tools simply select the version you want from drop down list options, refer picture below. (swift requires path to be xcode6's path).

对于命令行工具,只需从下拉列表选项中选择您想要的版本,请参考下面的图。(swift要求路径为xcode6的路径)。

我如何在终端使用swift ?

I will leave my previous answer below as well.

我也将把之前的答案留在下面。


what Kaan said and you can also use an apple script to make simple application so you can use it to switch back and forth.

Kaan说过,你也可以用苹果的脚本制作简单的应用程序,这样你就可以用它来回切换。

open apple script > paste this below code & export it as an application so with just one click you can switch to default path or beta path (to use swift)

打开苹果脚本>粘贴如下代码并将其导出为应用程序,只需单击一次即可切换到默认路径或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

then run > xcrun swift

然后运行> xcrun swift

disclaimer

免责声明

  1. the script assumes you have both xcode6-beta & xcode5 installed.
  2. 脚本假定您已经安装了xcode6-beta和xcode5。
  3. if you're a new developer who's trying out only xcode6beta you will not need any script or setting path manually. simply run xcrun swift as the path is already set for you.
  4. 如果您是一个只尝试xcode6beta的新开发人员,您将不需要任何脚本或手动设置路径。只需运行xcrun swift,因为路径已经为您设置。
  5. when xcode6 is finally released you will need to reset your path back to default from this simple app and never use it again.
  6. 当xcode6最终发布时,您将需要从这个简单的应用程序将路径重置为default,并且永远不再使用它。

#8


6  

After installing the official Xcode 6.1 release, there is a swift command in /usr/bin/swift.

在安装正式的Xcode 6.1版本之后,在/usr/bin/swift中有一个swift命令

Bear in mind that if you have a Python different from the Apple-supplied Python in the path, swift can fail with ImportError: No module named site. In that case, make sure that you do export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin before calling swift.

请记住,如果您有一条与路径上的苹果提供的Python不同的Python,那么swift可能会因为impor恐怖而失败:没有一个名为site的模块。在这种情况下,请确保在调用swift之前,您要执行export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin。

#9


5  

The xcrun command will use the DEVELOPER_DIR environment variable to override the currently selected Xcode installation (as set with xcode-select). You can use that to construct a single command that'll run swift on the command line and put you in the REPL. That looks like this:

xcrun命令将使用DEVELOPER_DIR环境变量覆盖当前选定的Xcode安装(如使用Xcode -select设置的那样)。您可以使用它构造一个命令,在命令行上运行swift并将您放入REPL中。像这样:

/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift 

and you can alias that to just 'swift':

你可以将其别名为“swift”

alias swift="/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift"

As an interesting side note, you can use the same kind of invocation to run a swift script just like you'd use bash or python by adding a -i:

有趣的是,您可以使用相同的调用来运行一个swift脚本,就像您使用bash或python时添加-i:

#!/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift -i

println("Hello World!")

Of course, once Xcode 6 is released officially and you switch to that as your default developer tools, you can drop the DEVELOPER_DIR=.. bits and just use "xcrun swift".

当然,一旦Xcode 6正式发布,并将其转换为默认的开发工具,就可以删除DEVELOPER_DIR=..使用“xcrun swift”即可。

#10


4  

make sure you install xcode 6.0 ,but not 6.1

确保您安装的是xcode 6.0,而不是6.1

If you get an error:

如果你犯了错误:

<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift

just run

你就跑

xcrun --sdk iphonesimulator8.0 swift

or you can

或者你可以

export SDKROOT="iphonesimulator8.0" 

and then

然后

xcrun swift

Use "xcodebuild -showsdks" to list the available SDK names.

使用“xcodebuild -showsdks”列出可用的SDK名称。

if you install xcode 6.1,just

如果您安装了xcode 6.1

sudo xcode-select -s /Applications/*your-Xcode-6.1-path.app*/Contents/Developer
xcrun swift

#11


2  

For XCode6, run these commands:

对于XCode6,运行以下命令:

$ sudo xcode-select -s /Applications/Xcode.app/Contents/Developer/

$ xcrun swift

If you get an error:

如果你犯了错误:

<unknown>:0: error: the SDK 'MacOSX10.9.sdk' does not support Swift

try:

试一试:

xcrun swift -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk

#12


1  

open Terminal,

打开终端,

$sudo xcode-select -switch /Applications/Xcode6-Beta6.app/Contents/Developer

$ sudo xcode-select开关/应用程序/ Xcode6-Beta6.app /内容/开发人员

Notice: The Xcode6-Beta6.app should be replaced to appropriate version you installed

注意:Xcode6-Beta6。应用程序应该被替换为您安装的适当版本

Then put this line alias swift='xcrun swift' to ~/.bash_profile

然后将这行别名swift='xcrun swift'放到~/.bash_profile中

And,

而且,

$source ~/.bash_profile

$源~ / . bash_profile

$swift

美元迅速

There you go!

你走吧!