views:

996

answers:

4

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!

A: 

NSInteger is just an int:

typedef int NSInteger;

In your first line of code:

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

All you are doing is assigning sequence to itself, in a round about way. And since it's not initialized, it might be any random number.

Eric Maxey
NSInteger isn't always an int. In 64-bit applications, it's a long.
BJ Homer
Sorry, the original example was flawed. I've made it clear(er) that sequence is a property of a MyProcess.
dfdumaresq
A: 

Depending on how the application is built, NSInteger might be 32 bits, or it might be 64 bits. If it's a 64-bit value, you'll need to do

NSLog(@"sequence = %qi", sequence)

so that it correctly treats sequence as a 64-bit value. Note, however, that this won't work for 32-bit applications; as far as I'm aware, there's no single format specifier that will work to print an NSInteger in both 32-bit and 64-bit worlds.

BJ Homer
thanks, that's handy to know.
dfdumaresq
A: 

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.

Chuck
Yes, it is an NSNumber (as declared by CoreData), and I suppose that answers my question... I was confused because the NSString properties can be printed without any fuss, but perhaps there's some added work done by the string formatter ("%@"). Thanks!
dfdumaresq
The `%@` specifier indicates an object — any object. You can also print an NSNumber that way. For example, `NSLog(@"%@, %@, %@!", [NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3])` will log "1, 2, 3!". It works by calling the `description` method of the object. The `%d` specifier is for ints.
Chuck
Thanks for this!
dfdumaresq
A: 

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.

Thomas Müller
Thanks! this (along with the Chuck's explanation of %@ above) both tells me what I was doing wrong and how to fix it.
dfdumaresq