【问题标题】:How to print int * & unsigned int* in NSLog?如何在 NSLog 中打印 int * & unsigned int*?
【发布时间】:2013-01-18 10:26:55
【问题描述】:

如何使用NSLog在日志中打印int*(整数指针)和unsigned int*

- (int) doSomethingWith:(unsigned int)Msg withWparam:(unsigned int*)wParam withParameter:(int *) lParam
{
    NSLog(@"MSg:%d wParam:%u lParam:%u",Msg,wParam,lParam);
//not working
    return 1;
}

警告: Format specifies type 'unsigned int' but the argument has type 'unsigned int *'

【问题讨论】:

  • @Downvoter 也请说出原因?
  • 我投了你的票,因为这里有愚蠢的巨魔,当他们不知道某事的答案时会投反对票。但是这个问题确实帮助了我,为此你得到了我的支持。

标签: objective-c nslog


【解决方案1】:

%d 用于int。而且参数是指针,所以使用*来访问指向的值。

NSLog(@"MSg:%d wParam:%u lParam:%d",Msg,*wParam,*lParam);

【讨论】:

  • +1 感谢@Joris,但如果 wParam 没有值(意味着 wParam 为零),它会崩溃。如何检查? // 现在使用:NSLog(@"MSG:%d wParam:%u lParam:%d",Msg,*wParam, *lParam);
  • 您必须在登录前检查:if (!wParam || !lParam) { /* invalid arguments */ }
【解决方案2】:

%@ 用于对象。 BOOL 不是对象。你应该使用%d
基于数据类型%@变化如下

For Strings you use %@
For int  you use %i
For float you use %f
For double you use %lf

【讨论】:

  • 那你为什么用 %i 作为 int
  • %d, %i 用于有符号整数,%u 用于无符号整数
猜你喜欢
  • 2012-11-11
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多