如何在iOS SDK中使用Swift REPL

12

我能否使用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)

2个回答

8
您可以通过在已附加到 iOS 应用程序进程(您的 Xcode 项目)的 lldb 中运行 repl 来实现此目标。
  1. Build project in Xcode, or:

    $ xcrun xcodebuild -configuration Debug -destination 'platform=iOS Simulator,name=iPhone 7,OS=10.3' clean build
    
  2. Start standalone lldb for your iOS project:

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

    You may find useful platform select ios-simulator and platform connect $UDID commands here.

  3. Run your iOS application in iOS simulator from Xcode

    • Or from command line:

      1. Boot simulator

        • From instruments:

          $ xcrun instruments -w "`xcrun instruments -s | grep 'iPhone 7 (10.3)' | head -1`"
          
        • Or as an application:

          $ open -a "Simulator" --args -CurrentDeviceUDID "`xcrun instruments -s | grep 'iPhone 7 (10.3)' | head -1 | sed -E -e 's/[^][]*\[([^][]*)\][^][]*/\1/g'`"
          
      2. Install the application on simulator, and launch it:

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

        Also, you can even use xcrun simctl launch --wait-for-debugger and start lldb later.

    • Or with ios-sim:

      1. Optionally boot simulator & install the application:

        $ 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. Launch it:

        $ ios-sim launch --devicetypeid 'iPhone-7, 10.3' $DerivedData/$AppName/Build/Products/Debug-iphonesimulator/$AppName.app
        
  4. Attach to process in iOS simulator in lldb:

    (lldb) continue
    (lldb) process interrupt
    
  5. Run swift repl.

    (lldb) repl
     1> import UIKit
     2> 
    

此外,与 Xcode 调试终端仿真器中的 swift repl 不同,这里具有工作源代码自动完成和命令历史记录导航。


1
这是一个惊人的答案。 - WestCoastProjects

4

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


2
这个改变了吗? - Raphael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接