Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change NSNumber value in XCode while debugging?

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.

like image 298
xmartin Avatar asked Jan 29 '26 20:01

xmartin


1 Answers

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.

like image 102
Martin R Avatar answered Feb 01 '26 14:02

Martin R