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'
Trip
  • 26,756
  • 46
  • 158
  • 277
  • 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 – Wain Apr 04 '16 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 – pbodsk Apr 04 '16 at 11:12

2 Answers2

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
Community
  • 1
  • 1
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • You can also enter the Swift REPL from within the debugger ([and return to lldb again](http://stackoverflow.com/questions/28585154/xcode-how-to-exit-lldb-swift-repl/28585961#28585961)). – Martin R Apr 04 '16 at 11:26
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.

Dan Halliday
  • 725
  • 6
  • 22
  • A fascinating. Does this work alongside a running app though? Can I use this with a debugger? – Trip Apr 04 '16 at 11:01
  • No, this is purely a [read-eval-print loop](https://en.wikipedia.org/wiki/Read–eval–print_loop). But the REPL can be started from inside the LLDB debugger as stated by @Sulthan. – Dan Halliday Apr 04 '16 at 11:44