TraderJoeChicago
TraderJoeChicago

Reputation: 6325

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 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?

Upvotes: 2

Views: 653

Answers (4)

VdesmedT
VdesmedT

Reputation: 9113

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...

Upvotes: 5

Stephen Furlani
Stephen Furlani

Reputation: 6866

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.

Upvotes: 1

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181430

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.

Upvotes: 3

vodkhang
vodkhang

Reputation: 18741

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";

Upvotes: 3

Related Questions