How can I use swift in Terminal?

后端 未结 13 556
北荒
北荒 2020-11-29 15:28

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

Command Line

Xcode’s debugger includes an inter

相关标签:
13条回答
  • 2020-11-29 16:09
    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.

    0 讨论(0)
  • 2020-11-29 16:09

    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.

    0 讨论(0)
  • 2020-11-29 16:10

    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)")
    
    0 讨论(0)
  • 2020-11-29 16:12

    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!

    0 讨论(0)
  • 2020-11-29 16:15

    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
    
    0 讨论(0)
  • 2020-11-29 16:20

    ** 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.
    0 讨论(0)
提交回复
热议问题