【问题标题】:How can I use Swift REPL with iOS SDK如何在 iOS SDK 中使用 Swift REPL
【发布时间】:2014-11-13 01:08:11
【问题描述】:

我可以使用 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 swift


    【解决方案1】:

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

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

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

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

      您可能会在此处找到有用的 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) continue
      (lldb) process interrupt
      
    5. 运行swiftrepl.

      (lldb) repl
       1> import UIKit
       2> 
      

    此外,与 Xcode 调试终端模拟器中的 swift repl 不同,这里我们有工作源自动完成和命令历史导航。

    【讨论】:

    • 这是一个了不起的答案。
    【解决方案2】:

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

    【讨论】:

    • 这有改变吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 2018-03-14
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多