开发者

Correctly debugging IPhone Apps on XCode

The line below has an error, can you see it with a naked eye?

NSString *title = [sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:"%@ button pressed.", title];

When the Iphone simulator executes this line it crashes without any other information. I must be missing something about debugging. The console showed me nothing.

I was able to figure out the error, my question is not about that. My question is why I get a hard crash with no help from XCode. Without any clue it took 开发者_Python百科me precious 5 minutes before I could realize my typo.

Any suggestions? Or when programming for IPhone I just need to be very careful with typos?

EDIT: I guess some people did not see it immediately like me. The error is the lack of '@' for the string constant. So now my question is why XCode/Simulator did not show me any kind of error message, just crashed without any clues. Am I missing something about debugging?


Objective-C does not strongly verify that the arguments you pass to messages are of the right type during compilation nor at runtime. It should gives you a warning though. Here you pass a c string instead of a NSString. Because NSString are objects (thus referenced using pointer), your method uses it as a pointer while you feed it with a simple string. You then probably try to access unaccessible memory blocks...


I think you miss a @ before the "%@ button pressed". The correct one should be:

NSString *newText = [[NSString alloc] initWithFormat:@"%@ button pressed.", title];

All the constant NSString should be @"SOMETHING HERE";


Check you compilation warnings. That's all you need. On the case you are showing, you will get a proper warning that will alert you that bad things might happen at that line.


I get the following Error when compiling your code:

error: cannot convert 'const char*' to 'NSString*' in argument passing

Not sure what you need to do to get it to show you that, I'm working in Obj-C++.

Try adding "-Wall" to your "OtherWarningFlags" under your target's build settings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜