objective-c 如何使用 NSLog 从 NSManagedObject 打印 NSInteger 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1528670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 22:19:33  来源:igfitidea点击:

How to print a NSInteger value from an NSManagedObject using NSLog

objective-ciphone

提问by dfdumaresq

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.

当我尝试将整数值打印到从 NSManagedObject 检索到的控制台时,它会显示一个 6 位或 8 位数字值(对象 ID?)。但是,如果我使用调试器“将描述打印到控制台”显示为我期望的个位数值。

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

例如,我将对象“序列”分配给 NSInteger,然后使用 NSLog 格式字符串显示:

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:

但是当我从调试器尝试“打印到控制台”时,我看到值 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!

感谢您的帮助!

回答by Thomas Müller

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

你从你的 NSManagedObject 得到的将是一个 NSNumber,我想。很容易打印:

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

or, if you really need the NSInteger:

或者,如果你真的需要 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.

(NSInteger)myProcess.sequence 实际上是获取 NSNumber 的内存地址。你不能只是将一个 NSNumber 转换成一个 NSInteger。

回答by Chuck

It sounds like myProcess.sequenceis 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.

听起来像是myProcess.sequenceNSNumber(对象)而不是 NSInteger(标量)。这将解释为什么它在对象的描述中正确显示,而不是当您明确尝试将其打印为整数时。

回答by BJ Homer

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

根据应用程序的构建方式,NSInteger 可能是 32 位,也可能是 64 位。如果是 64 位值,则需要执行

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

so that it correctly treats sequenceas 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.

以便它正确地视为sequence64 位值。但是请注意,这不适用于 32 位应用程序;据我所知,没有单一的格式说明符可以在 32 位和 64 位世界中打印 NSInteger。

回答by abtree

NSInteger is just an int:

NSInteger 只是一个整数:

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.

你所做的就是以一种迂回的方式将序列分配给它自己。而且由于它没有初始化,它可能是任何随机数。

回答by C?t?lin R?du??

Try this:

尝试这个:

NSLog(@"sequence = %li",(unsigned long)myProcess.sequence);