简体   繁体   English

如何在xCode和Objective-C中正确调试

[英]how to properly debug in xCode and Objective-C

I was wondering if there is an effective way to debug problems in xcode while coding in Objective-C. 我想知道在Objective-C中编码时是否存在有效的方法来调试xcode中的问题。 I create webpages 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. 我不断创建网页并使用jquery和javascript编写代码,您可以在代码中的不同位置设置各种警报框,以确定您的脚本是否正确执行了部分。 Can you do something like that in xcode to make sure that your script is executing methods properly and creating variables properly? 您可以在xcode中执行类似的操作以确保脚本正确执行方法并正确创建变量吗?

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. 您可以将光标悬停在IDE中的变量上以检查它们的值,查看当前的调用堆栈(以查看此处已调用此代码),并获取局部变量列表以帮助跟踪程序状态。 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. 通过'Stepping Over'(cmd + shift + o)逐行执行代码,执行当前行,'Stepping Into'(cmd_shift + i)进入当前代码行(如果它是函数),或者'Stepping Out'返回调用堆栈。

If you want to stick to 'old-school' printf style debugging, go with NSLog ing output to console. 如果要坚持使用“老式”的printf样式调试,请将NSLog ing输出转到控制台。

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 pi等于:3.14

Standard c formatters can be used in NSLog ie %d for int, %.2f for a float to 2 decimal places etc. Use %@ for NSString* s. 可以在NSLog中使用标准的c格式化程序,即%d表示int, %.2f表示浮点到小数点后两位。等等NSString* %@ NSString*使用%@

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. 请记住, NSLog将保留在生产代码中,除非您从发布版本(或类似的东西)中#IFDEF它,因此如果您不希望性能#IFDEF ,或者应用程序附带令人尴尬的控制台日志,您将需要删除它们。

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 :| 可以做到删除这样的事情:|

是的,调试器可以做你想做的所有事情(只需设置一些断点 - 右键单击​​你想要的地方 - 然后构建和调试)

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. 要查看应用程序的输出,在使用Xcode运行时,单击Run-> Console,您将看到应用程序的所有输出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM