I was wondering if there is an effective way to debug problems in xcode while coding in Objective-C. I create webp开发者_Go百科ages constantly and code in jquery and javascript where you can set various alert boxes in different places in your code to determine if your script is properly executing sections. Can you do something like that in xcode to make sure that your script is executing methods properly and creating variables properly?
Thanks
Use the debugger - that's what it is there for! Set breakpoints by clicking in the grey are next to the line of code you want to break on. When this line of code is going to be excuted, the debugger will kick in and highlight the current place in execution. You can hover the cursor over variables in the IDE to examine their values, view the current call-stack (to see here this code has been called from) and get a list of local variables to help track program state. You can modify variable properties here too which often makes debugging simpler.
Execute code line by line by 'Stepping Over' (cmd+shift+o), which executes the current line, 'Stepping Into' (cmd_shift+i) which steps into the current line of code (if it is a function), or 'Stepping Out' to return back up the call stack.
If you want to stick to 'old-school' printf style debugging, go with NSLog
ing output to console.
NSLog(@"this text appears");
prints the following to the console:
this text appears
To print some basic variable values:
CGFloat pi = 3.14;
NSString *aFloatString = [NSString stringWithFormat:@"%.2f", pi];
NSLog(@"pi is equal to: %@", aFloatString);
Prints:
pi is equa to: 3.14
Standard c formatters can be used in NSLog
i.e %d
for int, %.2f
for a float to 2 decimal places etc. Use %@
for NSString*
s.
Remember that NSLog
will remain in production code unless you #IFDEF
it out of release builds (or something similar), so if you don't want a performance hit, or embarrassing console logs to accompany the app you will want to remove them.
I've been known to litter functions that dump the following to console - and it isn't good:
OUTPUT:
Number of vertices is: 1200
<Requested reduction>
Can I kick it?
....
....
YES. I. CAN!
Number of vertices is: 800
Could have done with removing things like that :|
yes, the debugger can do all the things you want to do (just set some breakpoints - right click where you want them - then build&debug)
You can try writing to the console.
NSLog(@"Some status message here");
NSLog(@"The value of myObject is:%@", myObject);
To view the output of your application, while running with Xcode, click Run->Console and you will see all of the output from your application.
精彩评论