Can not display local variable at breakpoints

ok what changed in xCode7 / Swift that I can no longer use "po frame" to view the contents of a CGRect anylonger? The print statment works just fine in the code. How come I can't view that in the debugger console like I used to?


var frame = self.myLabel.frame
   
    frame.origin.x = self.startingFrame.origin.x + translation.x
    frame.origin.y = self.startingFrame.origin.y + translation.y
   
    print(frame)
   
    self.myLabel.frame = frame

yet in the debugger if I break on the self.myLabel.frame = frame statement and use po (or p or print) in the debugger I get:


(164.0, 323.0, 41.6666666666667, 20.3333333333333)
    (lldb) po frame
    error: <EXPR>:1:1: error: use of unresolved identifier 'frame'
    frame
    ^~~~~
    (lldb) p frame
    error: <EXPR>:1:1: error: use of unresolved identifier 'frame'
    frame
    ^~~~~
    (lldb) print frame
    error: <EXPR>:1:1: error: use of unresolved identifier 'frame'
    frame
    ^~~~~
    (lldb)

Accepted Reply

Ok this is odd. Today when I go into the project and try to display the variable, it works! I've rebooted since yesterday. Very odd. Will keep watching the next few days before closing this issue.

Replies

Is this a bug?

Try this:

  1. create a new project from the iOS > Single View Application target

  2. add the code below to the end of

    -[MainViewController viewDidLoad]
  3. set a breakpoint on the second

    print
  4. see if you can print

    frame
let frame = self.view.frame
print(frame)
print(frame)

I did this and it worked just fine (Xcode 7.0 on OS X 10.10.5 targeting the iOS 9.0 simulator). Please try it out and let us know how you get along.

Curiously, I originally tried this using a test project I have and it didn’t work. After some spelunking I found that this was related to the Optimization Level build setting, which should have been set to None (

SWIFT_OPTIMIZATION_LEVEL = -Onone
) for my Debug build configuration and yet wasn’t. Once I fixed that, I was able to print local variables in my test project as well.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I have been experiencing the same problem, and I have verified that I do not have optimization enabled for my builds. I tried a clean sample project and I can print local variables there, but I will try to pay closer attention to when they don't print in a more complicated project.


Another (possibly related?) problem is that even though -DDEBUG=1 is being passed in as an argument to my source code compiles, code that is inside of a #if DEBUG for a Swift source file is not being compiled.

Ok, with further digging, I realized that the default -DDEBUG=1 that comes with a Swift project is not the right way to pass that flag to Swift. You need to add "-D DEBUG" to "Other Swift Flags". The space between -D and DEBUG seems to be optional, but you CANNOT include the "=1". So, you can add -DDEBUG to Other Swift Flags, and leave the deafult one in the Preprocesor flags section, if you have both Objective-C and Swift code in your project. Changing the C Preprocessor one to remove the "=1" doesn't seem to work, so I'd just add it to Other Swift Flags.


It seems like this should be a default in newly-created Swift project.

Ok this is odd. Today when I go into the project and try to display the variable, it works! I've rebooted since yesterday. Very odd. Will keep watching the next few days before closing this issue.

It seems like this should be a default in newly-created Swift project.

You know what to do.

Share and Enjoy

Quinn "The Eskimo!"
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

That didn't work for me.


I noticed that I'm not able to debug certain types, as CGSize, CGRect, CGPoint... However I'm able to debug other struct types as String or my own types.


Bug report: #23217188

Hello,


Just created a bug (bugreport #24106425) for lldb.

lldb unable to print variable from breakpoint, while printing same variable from code (print statement) is working as expected.


Sample code below have the several asynchronous branches of code.

Setting breakpoints on every line where print statement is used and printing contents of variable someValue (po someValue) cause at least two lldb errors:


error: Execution was interrupted, reason: EXC_BAD_ACCESS (code=1, address=0x81000).

The process has been returned to the state before expression evaluation.


class ViewController: UIViewController {


  private lazy var session = NSURLSession.sharedSession()
  private lazy var operationQueue: NSOperationQueue = {
      let queue = NSOperationQueue()
      queue.maxConcurrentOperationCount = 1
      queue.qualityOfService = .UserInitiated
      return queue
  }()


  @IBAction private func performSayHello() {
      let task = session.dataTaskWithURL(NSURL(string: "https://www.apple.com")!) { [weak self] data, response, error in
        guard let s = self else {
            return
        }
        let someValue = "A"
        print(someValue, "-----", separator: "\n")
        s.performFilteringTask(someValue: someValue, completionHandler: { someValue in
            print(someValue, "-----", separator: "\n")
        })
      }
      task.resume()
  }


  private func performFilteringTask(someValue aValue: String, completionHandler: (String -> Void)?) {
      var someValue = ""
      let op = NSBlockOperation(block: {
        someValue = aValue + "+B"
        print(someValue, "-----", separator: "\n")
      })
      op.completionBlock = {
        dispatch_async(dispatch_get_main_queue()) { [weak self] in
            guard let s = self else {
              return
            }
            print(someValue, "-----", separator: "\n")
            s.performSomethingSpecial(someValue: someValue, completionHandler: completionHandler)
        }
      }
      operationQueue.addOperation(op)
  }


  private func performSomethingSpecial(someValue aValue: String, completionHandler: (String -> Void)?) {
      var someValue = ""
      dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0)) {
        someValue = aValue + "+C"
        print(someValue, "-----", separator: "\n")
        completionHandler?(someValue)
      }
  }
}

I had an older Swift project that somehow had the release and Debug Config set to the release optimization settings. Thanks, this solved my problem perfectly!