9

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!

dfdumaresq
  • 1,618
  • 4
  • 17
  • 22

5 Answers5

13

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
  • 15,565
  • 6
  • 41
  • 47
  • Thanks! this (along with the Chuck's explanation of %@ above) both tells me what I was doing wrong and how to fix it. – dfdumaresq Oct 07 '09 at 02:33
1

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
  • 234,037
  • 30
  • 302
  • 389
  • 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 Oct 07 '09 at 00:29
  • 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 Oct 07 '09 at 00:44
0

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
  • 48,806
  • 11
  • 116
  • 129
-1

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.

abtree
  • 325
  • 4
  • 10
-1

Try this:

NSLog(@"sequence = %li",(unsigned long)myProcess.sequence);
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
  • 2
    please improve your answer with some explanation, espacially compared to other answers which where given already – SBH Dec 09 '14 at 12:18