How to play interactively with objects in Xcode lldb swift repl?
Suppose I have a small program that collects a document from a database:
let errorDatabase
= NSError(domain: "dk.myproject.couchbase", code: 0, userInfo: nil)
let rev = database.existingDocumentWithID("_design/" + designDocName)
// in xcode I set a break point here
assert(rev != nil)
if rev == nil {
promise.failure(errorDatabase)
}
Then I insert a breakpoint, run the program and after that I can do:
(lldb) po rev
0x00007fad21df61c0
{
ObjectiveC.NSObject = {...}
}
(lldb) print rev.properties["views"]
(AnyObject?) $R29 = Some {
...
Perfect lets you enter repl
and play with an object rev
:
(lldb) repl
6> rev
repl.swift:6:1: error: use of unresolved identifier 'rev'
rev
^
I may have incorrect expectations for a quick replacement - I'm expecting some kind of python, nodejs or scala repl. behavior where I can play with objects, etc.
Any hints?
source to share
I was hoping for the same the first time I typed repl
into LLDB, but I soon found that, unfortunately, you cannot do this.
The replica inside LLDB turns out to be running in a module injected at the top level. This way, you can define top-level objects and functions within the replica, which are then displayed in the "normal" lldb:
(lldb) repl
1> func pt() -> CGPoint {
2. return CGPointZero
3. }
4> :
(lldb) po pt()
(0.0, 0.0)
... but the opposite is not true: you cannot, inside repl, see local variables while you are paused, since they are clearly not in top-level scope.
Note, however, that you can make assignments in an expression expr
. This way, you can change the value of a local variable, properties of an existing object, etc., simply by specifying expr
which is followed by the assignment and that it happens in the context in which you are suspended.
For example, let's say I'm in the middle of creating a pan gesture recognizer, and I stop at a breakpoint at this line:
p.edges = UIRectEdge.Right
Now:
(lldb) th step-over
(lldb) expr p.edges = UIRectEdge.Left
(lldb) continue
And now the app is running, but the gesture recognizer works when scrolling to the left instead of right.
source to share