我如何使用iOS SDK的Swift REPL

我可以用iOS SDK运行Swift REPL吗?

我想在REPL中导入和使用UIKit ,但没有成功。

 $ xcrun --sdk iphonesimulator8.1 --show-sdk-path /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.1.sdk $ xcrun --sdk iphonesimulator8.1 swift Welcome to Swift! Type :help for assistance. 1> import UIKit /var/folders/kb/xgglxb597sv6h8b744d5vft00000gn/T/lldb/92014/repl1.swift:2:8: error: no such module 'UIKit' import UIKit ^ $ swift -sdk `xcrun --sdk iphonesimulator8.1 --show-sdk-path` Welcome to Swift! Type :help for assistance. 1> import UIKit /var/folders/kb/xgglxb597sv6h8b744d5vft00000gn/T/lldb/91881/repl1.swift:2:8: error: no such module 'UIKit' import UIKit ^ 1> import Cocoa 2> 

我正在使用Xcode版本6.1(6A1052d)

您可以通过运行附加到iOS应用程序进程(您的Xcode项目)的lldb repl来实现它。

  1. 在Xcode中构build项目,或者:

     $ xcrun xcodebuild -configuration Debug -destination 'platform=iOS Simulator,name=iPhone 7,OS=10.3' clean build 
  2. 为您的iOS项目启动独立的 lldb

     $ xcrun lldb -- $DerivedData/$AppName/Build/Products/Debug-iphonesimulator/$AppName.app (lldb) process attach --name '$AppName' --waitfor 

    您可以在这里find有用的platform select ios-simulatorplatform connect $UDID命令。

  3. 在Xcode的iOS模拟器中运行你的iOS应用程序

    • 或从命令行:

      1. 启动模拟器

        • instruments

           $ xcrun instruments -w "`xcrun instruments -s | grep 'iPhone 7 (10.3)' | head -1`" 
        • 或作为一个应用程序:

           $ open -a "Simulator" --args -CurrentDeviceUDID "`xcrun instruments -s | grep 'iPhone 7 (10.3)' | head -1 | sed -E -e 's/[^][]*\[([^][]*)\][^][]*/\1/g'`" 
      2. 在模拟器上安装应用程序,并启动它:

         $ xcrun simctl install booted $DerivedData/$AppName/Build/Products/Debug-iphonesimulator/$AppName.app $ xcrun simctl launch booted $AppBundleID 

        另外,你甚至可以使用xcrun simctl launch --wait-for-debugger ,稍后再启动lldb

    • 或者用ios-sim :

      1. 可选启动模拟器并安装应用程序:

         $ ios-sim start --devicetypeid 'iPhone-7, 10.3' $ ios-sim install --devicetypeid 'iPhone-7, 10.3' $DerivedData/$AppName/Build/Products/Debug-iphonesimulator/$AppName.app 
      2. 启动它:

         $ ios-sim launch --devicetypeid 'iPhone-7, 10.3' $DerivedData/$AppName/Build/Products/Debug-iphonesimulator/$AppName.app 
  4. 附加到在lldb iOS模拟器中进行lldb

     (lldb) continue (lldb) process interrupt 
  5. 运行swift repl。

     (lldb) repl 1> import UIKit 2> 

此外,与Xcodedebuggingterminal仿真器中的swift repl相反,这里我们有源代码自动完成和命令历史导航。

Swift REPL目前不支持iOS设备或iOS模拟器。