How to print NSInteger value from NSManagedObject using NSLog

When I try to print an integer value on a console that is retrieved from NSManagedObject, it displays a value of 6 or 8 digits (object identifier?). However, if I use a debugger, "Print Description to Console" appears as the single-valued value that I expect.

For example, I assign a "sequence" to an NSInteger object 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:

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

But when I try to "Print to Console" from the debugger, I see a value of 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!

+7
objective-c iphone
source share
5 answers

What you get from your NSManagedObject will be NSNumber, I think. It is easy to print that:

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

or if you really need NSInteger:

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

I think in this piece of code

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

(NSInteger) myProcess.sequence actually gets the memory address of NSNumber. You cannot just specify NSNumber in NSInteger.

+11
source share

It looks like myProcess.sequence is an NSNumber (object), not an NSInteger (scalar). This explains why it appears correctly in the description of the object, but not when you are explicitly trying to print it as an integer.

+1
source share

Depending on how the application is created, NSInteger may be 32 bits or may be 64 bits. If this is a 64 bit value, you will need to do

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

so that it correctly processes the sequence as a 64-bit value. Please note, however, that this will not work for 32-bit applications; as far as I know, there is no single format specifier that will work for NSInteger printing in 32-bit and 64-bit worlds.

0
source share

NSInteger is just an int:

 typedef int NSInteger; 

In your first line of code:

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

All you do is assign a sequence to yourself in a round. And since it is not initialized, it can be any random number.

-one
source share

Try the following:

 NSLog(@"sequence = %li",(unsigned long)myProcess.sequence); 
-one
source share

All Articles