您的位置:首页 > 移动开发 > IOS开发

iOS指南系列:如何解决奔溃问题-关于内存访问续

2012-11-07 16:58 561 查看


Push the Button

现在的应用程序工程 - 或者至少是没有问题的开始 - ,点击该按钮运行。





Woah! 程序又崩溃了 SIGABRT ,还在 main.m.

在调试窗格中的错误消息是:

Problems[6579:f803] -[MainViewController buttonTapped]: unrecognized selector sent
to instance 0x6e44850
堆栈跟踪不是太清晰,它列出了一大堆有关单程或其他发送事件和执行行动的方法,但你已经知道,行动参与。毕竟,你可以找到一个被称为IBAction方法的UIButton和结果。可以看到NSobject Performaselector执行了一个方法:

当然,你见过这个错误讯息。”一种方法被称为不存在的“。这一次的目标对象是MainViewController,看起来是正确的操作方法之一,因为通常包含按钮在视图控制器。如果你在MainViewController.h看,IBAction方法也确实是存在的:

- (IBAction)buttonTapped:(id)sender;
我们可以和之前的步骤一样,继续点击执行,直到错误被完全抛出:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewController buttonTapped]: unrecognized selector sent to instance 0x6b4b1f0'
这次看到多一个信息,参数不合法!

或者说问题就是它吗?错误消息说,方法的名称是buttonTapped,但MainViewController有方法命名buttonTapped:,注意在最后一个冒号,因为这个方法接受一个参数(名为“sender”)。另一方面,从错误信息的方法的名称,不包括冒号,因此不带任何参数。该方法的签名,而似乎看起来像这样:

- (IBAction)buttonTapped;
这里发生了什么?最初的方法没有参数的定义方式,也是允许的操作方法。而且在连接触摸按钮的内部事件和storyboard的button时候,似乎就是没有参数的方式。然而,一段时间后,该方法的签名被更改,包括“sender”参数,但没有更新相应的sences。

你可以看到的storyboard,按钮连接inspector(MainviewController-buttontapped):





首先断开触摸内部事件(点击小X),然后将它连接主视图控制器,但这个时候选择的buttonTapped的方法是现有的。链接后,请注意,在连接inspector,现在 方法的名称后有个小冒号(表明是带参数的)。

运行的应用程序,再点击按钮。什么?在main函数中,你又得到“无法识别的selector”的消息,虽然这一次正确识别作为buttonTapped:的 方法

Problems[6675:f803] -[MainViewController buttonTapped:]: unrecognized selector sent
to instance 0x6b6c7f0
If you look closely, the compiler warnings should point you to the solution again. Xcode complains that the implementation of MainViewController is incomplete. Specifically, the method definition for buttonTapped:is
not found.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewControllerbuttonTapped:]: unrecognizedselector sent to instance 0x6d37a00'





轮到检查 MainViewController.m. 看起来似乎有定义(我也没有发现这个拼写错误) buttonTapped: 但是等一下…
好像不对头啊:

- (void)butonTapped:(id)sender
很容易解决,修改下:

- (void)buttonTapped:(id)sender
注意,这里就没有必要申明为IBAction, 如果你愿意,当然也可以!

Note: This sort of thing is easy to catch if
you’re paying attention to the compiler warnings. Personally, I treat all warnings as fatal errors (there is even an option for this in the Build Settings screen in Xcode) and I’ll
fix each and every one of them before running the app. Xcode is pretty good at pointing out silly mistakes such as these, and it’s wise to pay attention to these hints.

我的一个结论:
1. 绝对重视编译错误,fix他们
2. 多阅读代码
3. 把告警的地方的信息展开,看详细内容
忘了:检查inspector/outlet/view/event connection
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: