Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Swift REPL with iOS SDK

Tags:

ios

swift

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-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>  

I'm using Xcode Version 6.1 (6A1052d)

like image 906
rintaro Avatar asked Nov 13 '14 01:11

rintaro


People also ask

How do I use the Swift REPL?

All you need to do is type Swift statements and the REPL will immediately execute your code. Expression results are automatically formatted and displayed along with their type, as are the results of both variable and constant declarations. Console output flows naturally within the interactive session:

What is Swift language for iOS?

Swift is a powerful and intuitive programming language for iOS, iPadOS, macOS, tvOS, and watchOS. Writing Swift code is interactive and fun, the syntax is concise yet expressive, and Swift includes modern features developers love. Swift code is safe by design, yet also produces software that runs lightning-fast.

What is swift?

Swift is a powerful and intuitive programming language for macOS, iOS, watchOS, tvOS and beyond.

How do I link a swift project to a Swift Package?

Find and copy the absolute path to your package, then go to File > Swift Packages > Add Package Dependency…, and enter file:// {path} as the url. At this point you can choose what branch, commit id, or version you want to use. Continue and it will automatically install and link your project as well as any dependencies.


2 Answers

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.

like image 174
Netsu Avatar answered Oct 17 '22 11:10

Netsu


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

like image 42
Greg Parker Avatar answered Oct 17 '22 12:10

Greg Parker