Programing

터미널에서 스위프트를 어떻게 사용합니까?

crosscheck 2020. 8. 4. 22:54
반응형

터미널에서 스위프트를 어떻게 사용합니까?


Xcode 6의 새로운 기능을 읽었습니다 . 이 기사에서는 Xcode 6에 대한 새로운 기능을 소개하며 다음과 같이 말합니다.

커맨드 라인

Xcode의 디버거에는 대화식 버전의 Swift 언어 (REPL (Read-Eval-Print-Loop))가 포함되어 있습니다. Swift 구문을 사용하여 실행중인 앱을 평가하고 상호 작용하거나 스크립트와 유사한 환경에서 새 코드를 작성하십시오. REPL은 Xcode 콘솔의 LLDB 또는 터미널에서 사용할 수 있습니다.

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

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$ 

터미널에서 Swift를 실행하는 것과 같은 방식으로 스크립트를 실행할 수도 있습니다. 다음 shebang을 사용하고 스크립트를 실행하십시오. ( Swift의 제작자 인 Chris Lattner에 따르면)

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

명령 행 도구가 설치된 Xcode 6.1.1에서는 /usr/bin/swift다음 방법을 직접 참조하여 스크립트를 실행할 수 있습니다 .

#!/usr/bin/swift

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

간단한 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 에서 제공하는 스냅 샷을 사용하려면 여기서 설치를 놓치지 않아야 합니다 .


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

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.

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


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!


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>

참고URL : https://stackoverflow.com/questions/24011120/how-can-i-use-swift-in-terminal

반응형