How to print a NSInteger value from an NSManagedObject using NSLog

前端 未结 5 1337
北恋
北恋 2021-01-12 06:15

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 debug

5条回答
  •  独厮守ぢ
    2021-01-12 06:36

    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.

提交回复
热议问题