How can I use Swift REPL with iOS SDK

前端 未结 2 1354
再見小時候
再見小時候 2020-12-05 08:40

Can I run Swift REPL with iOS SDK?

I want to import and use UIKit in REPL, but no success.

$ xcrun --sdk iphonesimulator8.1 --show-sdk-p         


        
相关标签:
2条回答
  • 2020-12-05 09:10

    The Swift REPL currently does not support iOS device or iOS simulator.

    0 讨论(0)
  • 2020-12-05 09:21

    You may achieve it by running repl from lldb, which attached to iOS application process (of your Xcode project).

    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> 
      

    Furthermore, as opposed to swift repl in Xcode debug terminal emulator, here we have working source autocompletion and command history navigation.

    0 讨论(0)
提交回复
热议问题