0

One of the best ways for me to work with Ruby was handling data in real time. In the debugger, I could set new variables, assign them, and manipulate the code.

Is this possible in xCode's Swift/Obj-C? It seems when I try to instantiate new variables, they are never accessible. Perhaps my syntax is wrong?

>> po let my_numb = 5
>> po my_numb

error: <EXPR>:1:1: error: use of unresolved identifier 'my_numb'
2
  • you can edit existing variables and interrogate data but you can't inject arbitrary code like that, what example situation are you trying to deal with, it seems like a playground may be what you're looking for Commented Apr 4, 2016 at 10:52
  • 1
    I agree with @Wain. If you just want to toy around with some code and see the outcome straight away playgrounds are an excellent way to do so Commented Apr 4, 2016 at 11:12

2 Answers 2

2

The (LLDB) debugger is not really a Swift REPL but you can create convenience variables there, however they have to be prefixed with $:

expr var $foo: Int = 10 
p $foo

Also see Martin R's answer about entering REPL from the debugger:

(lldb) repl                  // enter REPL
(lldb) let my_numb: Int = 10 // create Swift variable
(lldb) :                     // exit REPL
(lldb) p my_numb             // print variable

(Int) $R0 = 10
Sign up to request clarification or add additional context in comments.

1 Comment

You can also enter the Swift REPL from within the debugger (and return to lldb again).
1

For Swift, you can use the Swift REPL, which is exactly like IRB for Ruby.

You'll need to install Swift separately from the Xcode tools. Instructions to do that can be found here: https://swift.org/getting-started/#installing-swift. The Swift REPL is installed along with Xcode.

2 Comments

A fascinating. Does this work alongside a running app though? Can I use this with a debugger?
No, this is purely a read-eval-print loop. But the REPL can be started from inside the LLDB debugger as stated by @Sulthan.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.