如何在终端使用迅捷?

这篇文章介绍了 Xcode 6的一些新特性,它说:

命令行

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

我想知道如何得到 REPL?

84373 次浏览
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.

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

** update as of xcode6 beta 4 **

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

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).

command line tools screen

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.

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)

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

disclaimer

  1. the script assumes you have both xcode6-beta & xcode5 installed.
  2. 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.
  3. when xcode6 is finally released you will need to reset your path back to default from this simple app and never use it again.

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)

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

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:

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

and you can alias that to just '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:

#!/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".

Step 1: Open Terminal
Step 2: Type "swift"
Step 3: There's no step 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$

open Terminal,

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

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

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

And,

$source ~/.bash_profile

$swift

There you go!

For XCode6, run these commands:

$ 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

make sure you install xcode 6.0 ,but not 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.

if you install xcode 6.1,just

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

After installing the official Xcode 6.1 release, there is a swift command in /usr/bin/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.

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

#!/usr/bin/swift


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

If any one cares a simple Swift script shebang:

#!/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)

#!/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

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.


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

With the help of Swift REPL(Read Eval Print Loop).

Developers familiar with interpreted languages will feel comfortable in this command-line environment, and even experienced developers will find a few unique features

Launch Terminal.app and type swift and press enter. You’ll then be in the Swift REPL.

        1> print("Hello Swift REPL")
Hello Swift REPL
2> 10 + 20
$R0: Int = 30
3> var name = "Yogendra Singh"
name: String = "Yogendra Singh"
4> print(name)
Yogendra Singh
5>