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

来源:互联网 发布:淘宝延长收货三个月 编辑:程序博客网 时间:2024/06/11 14:53

Push the Button

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

The app finally starts up.

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

调试窗格错误消息

Problems[6579:f803] -[MainViewController buttonTapped]: unrecognized selector sentto instance 0x6e44850

堆栈跟踪不是太清晰,它列出了一大堆有关单程或其他发送事件执行行动方法,但你已经知道行动参与毕竟你可以找到一称为IBAction方法的UIButton结果。可以看到NSobject Performaselector执行了一个方法:
当然,你见过这个错误讯息。”一种方法被称为不存在的“这一次目标对象MainViewController看起来是正确的操作方法之一,因为通常包含按钮视图控制器如果MainViewController.hIBAction方法也确实是存在的

- (IBAction)buttonTapped:(id)sender;

我们可以和之前的步骤一样,继续点击执行,直到错误被完全抛出:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainViewController buttonTapped]: unrecognized selector sent to instance 0x6b4b1f0'

这次看到多一个信息,参数不合法!

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

- (IBAction)buttonTapped;

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

The button's connections in the storyboard.

 

首先断开触摸内部事件(点击小X然后将它连接主视图控制器,但这个时候选择buttonTapped方法是现有的。链接后,注意连接inspector,现在 方法的名称后有个小冒号(表明是带参数的)
运行的应用程序点击按钮什么?在main函数中,你又得到无法识别的selector”的消息虽然这一次正确识别作为buttonTapped:的 方法

Problems[6675:f803] -[MainViewController buttonTapped:]: unrecognized selector sentto 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'

Xcode shows an incomplete implementation warning.

轮到检查 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
原创粉丝点击