有没有办法在Objective-C中将消息发送到零? [英] Is there a way to trap messages sent to nil in Objective-C?

查看:133
本文介绍了有没有办法在Objective-C中将消息发送到零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚被一个令人讨厌的错误所困扰, Objective-C中的发送消息到nil是行。



我看过发送消息到nil?,并且共识似乎是在Objective-C中我们如何滚动。



现在,也许我在Objective-C上没有足够的经验,但是看起来这样做是有用的,因为我不能想到这个应该发生的好原因大部分的时间。然而,它可能只是一个编码成语,我还没有习惯。



所以除了这样检查无处不在:

  assert(object!= nil); 
[object message];

有没有办法使运行时陷阱这个条件,并在对象是否为nil?

解决方案

您可以使用(请参阅dtrace解决方案的注释).bake / 2008/01/02 / objective-c-logging-messages-to-nil /rel =noreferrer 。

  pid $ 1 :: objc_msgSend:entry 
/ arg0 == 0 /
{
ustack();
}


I've just been bitten by an annoying bug that was made obscure by the "send message to nil is ok" behaviour in Objective-C.

I've seen Sending a message to nil?, and the consensus seems to be 'thats how we roll' in Objective-C.

Now, maybe I don't have enough experience in Objective-C, but it seems like it would be useful to trap this, because I can't think of a good reason why this should be happening most of the time. However, It could just be a coding idiom I'm not used to yet.

So other than checking for nil everywhere like so:

assert( object != nil );
[object message];

Is there a way to make the runtime trap this condition, and warn when object is nil?

解决方案

You could use a ObjC undocumented trick or dtrace (see the comments for the dtrace solution).

pid$1::objc_msgSend:entry
/arg0==0/
{
  ustack();
}

这篇关于有没有办法在Objective-C中将消息发送到零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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