3

My class has a member variable which is a NSNumber, like so:

@interface C : NSObject {
    NSNumber* _n;
}

During debugging, I am stopped at a breakpoint, and I want to change the value of the NSNumber. How can I do it?

I tried the XCode variables window, but that does not work.

I tried the XCode debug console, for example

expr _n = @1

but that gives the bizarre message error: assigning to 'NSNumber *' from incompatible type 'NSNumber *' -- no kidding! Try it.

I also tried

expr _n = [NSNumber numberWithInt:1]

but that gives the same thing.

2 Answers 2

5

This worked for me:

(lldb) expr -- _n = (NSNumber *)[NSNumber numberWithInt:123]
(NSNumber *) $0 = 0x0000000000007b83 (int)123
(lldb) po _n
(NSNumber *) $1 = 0x0000000000007b83 123

The -- is required to mark the end of command options and beginning of "raw" input.

Strictly speaking, this does not change the value of the existing NSNumber, but assigns a new NSNumber object to _n. But NSNumber objects are immutable, so changing their value is not possible anyway.

Sign up to request clarification or add additional context in comments.

8 Comments

Can you share any links on good tutorials for learning the debugger in general?
@JRG-Developer: I must admit that for me working with the new lldb is often "try and error". But perhaps this thread helps: stackoverflow.com/questions/12974455/….
Looks like boxing syntax works here too. I tried po @(1.5) and got (NSNumber *) $1 = 0x000000010054a520 1.5
@nielsbot: I had tried only @123, which does not work in the debugger. So thanks for the feedback, I can add that to the answer if that is OK with you.
Sure, I don't mind. I just assumed the parentheses were required--I've never used any other syntax.
|
1

Try to use this line:

po _n = @(123)

Comments

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.