Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a NSInteger value from an NSManagedObject using NSLog

When I try to print an integer value to the console that is retrieved from an NSManagedObject, it displays a 6 or 8 digit value (the object ID?). However, if I use the debugger 'Print Description to Console' is shows up as the single digit value I expect.

For example, I assign the object 'sequence' to an NSInteger and then display using an NSLog format string:

MyProcess *myProcess = [array objectAtIndex:i];
NSInteger sequence = [[NSNumber numberWithInteger:(NSInteger)myProcess.sequence] intValue];
NSLog(@"sequence = %d",myProcess.sequence);

Console output is:

2009-10-06 16:11:05.871 MyProcess[33185:20b] sequence = 565256

But when I try 'Print to Console' from the debugger, I see the value 1:

<MyStoryImage: 0x3f59a80> (entity: MyObject; id: 0x3f2d540 <x-coredata://FF21959A-  4B67-4587-A25F-66A7B8139DFA/MyProcess/p2> ; data: {
sequence = 1;
<x-coredata://FF21959A-4B67-4587-A25F-66A7B8139DFA/MyProcess/p1>;
})

Your help is appreciated!

like image 428
dfdumaresq Avatar asked Oct 06 '09 23:10

dfdumaresq


2 Answers

What you get from your NSManagedObject would be a NSNumber, I think. It's easy to print that:

MyProcess *myProcess = [array objectAtIndex:i];
NSLog(@"sequence = %@", myProcess.sequence);

or, if you really need the NSInteger:

MyProcess *myProcess = [array objectAtIndex:i];
NSLog(@"sequence = %i", [myProcess.sequence integerValue]);

I think that in this bit of code

NSInteger sequence = [[NSNumber numberWithInteger:(NSInteger)myProcess.sequence] intValue];

the (NSInteger)myProcess.sequence actually gets the memory address of the NSNumber. You can't just cast an NSNumber into an NSInteger.

like image 88
Thomas Müller Avatar answered Oct 20 '22 00:10

Thomas Müller


It sounds like myProcess.sequence is an NSNumber (object) rather than an NSInteger (scalar). That would explain why it shows up correctly in an object's description but not when you explicitly try to print it as an integer.

like image 23
Chuck Avatar answered Oct 20 '22 00:10

Chuck