views:

124

answers:

4

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?

+3  A: 

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

vodkhang
Ok. Nice eye. But the question is why I get no help from XCode/Simulator to spot that bug. It just hard crashes. Am I missing something about debugging on XCode / Simulator?
Sergio Oliveira Jr.
downvotes are inappropriate, since the OP wouldn't tell us the error, this is good information.
KevinDTimm
why this is downvoted? "can you see it with a naked eye" is ended with a question mark and the ans is made before the EDIT in the question.
taskinoor
+5  A: 

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), you method use it as a pointer while you feed it with a simple string. You then probably try to access unaccessible memory blocks...

VdesmedT
Thanks. I guess I am not missing anything and this is how it should work. Hard crash. Not even the line where it hard crashes is given. I had to use breakpoints. This questions is just to realize whether I have to get used to this painful debugging process or if there are better ways to debug that on XCode.
Sergio Oliveira Jr.
I'm curious why you did not received any warning. I typed your code in Xcode on my machine just to be sure and it gave me a "warning: passing argument 1 of 'initWithFormat:' from incompatible pointer type" warning.
VdesmedT
Agreed. I got an error, which I posted below.
Stephen Furlani
+3  A: 

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.

Pablo Santa Cruz
+1  A: 

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.

Stephen Furlani
I get the warning, but not the error. When the app crashes it does not tell me the line where it crashed. So I guess that is expected. I just need to look for warnings and be very careful with memory crashes. Coming from Java that's not natural, but I can live with that.
Sergio Oliveira Jr.
'not natural' would be a misnomer. a different level of interaction with the machine by your programming language will make errors and warnings from the compiler far more 'important'. the fact that you ignored a warning about pointers is your first and greatest learning experience while developing at this level. heed these messages in the future. the debugger is not at fault here, the Debugger is :)
KevinDTimm
I agree Kevin. Lesson learned. Thanks!
Sergio Oliveira Jr.
glad I could help. Obj-C is definitely a different monster, and take my advice - don't try a program in Obj-C++!! That really messes stuff up.
Stephen Furlani