首页 文章

如何在终端中使用swift?

提问于
浏览
105

我读了What's new in Xcode 6 . 本文介绍了一些关于Xcode 6的新功能,它说:

命令行Xcode的调试器包括Swift语言的交互式版本,称为REPL(读取 - 评估 - 打印 - 循环) . 使用Swift语法评估正在运行的应用程序并与之交互,或者在类似脚本的环境中编写新代码 . REPL可以从LLDB内部的Xcode控制台或终端获得 .

我想知道如何获得REPL?

12 回答

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

    然后你可以做其中一个:

    xcrun swift 
    lldb --repl
    

    从Xcode 6.1开始 - 在终端中输入 swift 也会启动REPL .

  • 4

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

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

    第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$
    
  • 2

    与从终端运行Swift的方式相同,您也可以执行脚本 . 只需使用以下shebang,然后运行您的脚本 . (As per Chris Lattner,斯威夫特的创造者)

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

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

    #!/usr/bin/swift
    
    let variable: String = "string"
    print("Test \(variable)")
    
  • 58

    如果有人关心一个简单的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提供的快照,则不应错过Installation here .


    首先由我回答Run swift script from Xcode iOS project as build phase

  • 5

    **更新xcode6 beta 4 **

    这也可以在xcode首选项上完成 . 只需转到xcode - > preferences - > locations .

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

    command line tools screen

    我也会在下面留下我之前的答案 .


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

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

    然后运行> xcrun swift

    disclaimer

    • 该脚本假设您同时安装了xcode6-beta和xcode5 .

    • 如果你're a new developer who' s试用 only xcode6beta你不需要手动任何脚本或设置路径 . 只需运行 xcrun swift ,因为已经为您设置了路径 .

    • 当xcode6最终发布时,你需要从这个简单的应用程序重置你的路径回到 default ,永远不要再使用它 .

  • 39

    安装官方Xcode 6.1版本后, /usr/bin/swift 中有一个 swift 命令 .

    请记住,如果您在路径中使用的Python与Apple提供的Python不同, swift 可能会因 ImportError: No module named site 而失败 . 在这种情况下,请确保在调用 swift 之前执行 export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/sbin .

  • 132

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

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

    你可以将其别名为'swift':

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

    作为一个有趣的附注,您可以使用相同类型的调用来运行swift脚本,就像您使用bash或python一样添加-i:

    #!/usr/bin/env DEVELOPER_DIR=/Applications/Xcode6-Beta.app/Contents/Developer xcrun swift -i
    
    println("Hello World!")
    

    当然,一旦正式发布Xcode 6并将其切换为默认的开发人员工具,您可以删除DEVELOPER_DIR = ..位并使用“xcrun swift” .

  • 8

    确保安装 xcode 6.0 ,但不安装 6.1

    如果您收到错误:

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

    赶紧跑

    xcrun --sdk iphonesimulator8.0 swift
    

    或者你可以

    export SDKROOT="iphonesimulator8.0"
    

    然后

    xcrun swift
    

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

    如果您安装 xcode 6.1 ,只需

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

    对于XCode6,请运行以下命令:

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

    如果您收到错误:

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

    尝试:

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

    开放式终端,

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

    Notice: 应将 Xcode6-Beta6.app 替换为您安装的相应版本

    然后把这一行 alias swift='xcrun swift' 改为 ~/.bash_profile

    和,

    $ source ~/.bash_profile

    $ swift

    你去!

相关问题