How can I use swift in Terminal?

…衆ロ難τιáo~ 提交于 2019-11-27 02:27:34
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.

Sebastien Windal

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

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$ 

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
Markus Rautopuro

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)")
Puttin

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

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

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.

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.

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

liu lei

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

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

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!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!