如何使用NSLog从NSManagedObject打印NSInteger值 [英] How to print a NSInteger value from an NSManagedObject using NSLog

查看:132
本文介绍了如何使用NSLog从NSManagedObject打印NSInteger值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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);

控制台输出为:

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

但是当我从调试器尝试打印到控制台时,我看到值1:

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>;
})

感谢您的帮助!

推荐答案

我认为您从NSManagedObject获得的内容将是NSNumber。
很容易打印出来:

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);

或者,如果你真的需要NSInteger:

or, if you really need the NSInteger:

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

我认为在这段代码中

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

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

the (NSInteger)myProcess.sequence actually gets the memory address of the NSNumber. You can't just cast an NSNumber into an NSInteger.

这篇关于如何使用NSLog从NSManagedObject打印NSInteger值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆