`
hua397
  • 浏览: 53675 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

debug小记

阅读更多

    编程难免有bug,一些隐蔽的bug往往让人比较烦恼,初学者更是如此,俺经过亲身经历,整理了一些debug的小技巧,希望对初学者有所帮助,高手拍砖也欢迎哈

 

1.最常用的肯定是NSLog了,可以查看各种变量在各个状态的值,而且对于程序流程的整理也很有帮助

 

2.单步调试,一般鼠标悬浮在变量上就可以快速查看变量的状态,俺一般看这个变量的地址,就是0x开头的,主要是看它是否为nil,nil的地址是0x0,字符串可以看具体值,而字典和数组却只显示count,即有多少个元素或键值对,如果要查看整个数组或字典的话,此时是不行的(呵呵,方法在后面)

俺主要想说的其实是后面这几点,呵呵

 

3.debuger在单步调试的时候会出现,三部分,俺初学的时候只用下面的单步调试,直观快捷


 

3-1.后来发现:有个比较快捷的方法查看数组和字典,右上角的窗口里,有Arguments和locals(俺常用),在Arguments的self或loacls里有你想查看的数组的话,选中它,右键菜单上有print description to console,可以很方便的在console里查看数组和字典的值,当然像文件夹路径这种长字符串更不在话下了

 

3-2.左上角的窗口以前看上去好复杂,虽然现在看还是复杂,但俺看出了点门道,特别是在interrupt的时候,报错状态下,左上角的第一行显示????,其实左上角是倒序显示的,就是说最后执行的语句是在第一句,而已经执行的是显示在下面,有的时候,???下面有一些是显示"方法的调用",即[object message],这很关键,可以找到距离报错的地点最近的地方,经过俺的实践,常常就是在那解决bug的

 

4.也是在报错状态,console一般会显示错误信息,常见的unregnise selector这些看多了也就熟悉了,有的时候会显示Call stack at first throw这种也是倒着看,先执行的在下面显示,定位到离错误最近的地方解决bug,如:

 

2010-10-16 20:14:13.633 Universiade 2011[5293:207] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<MoreSecondViewController_ser 0x6d49bf0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key mytableView.'
*** Call stack at first throw:
(
	0   CoreFoundation                      0x0264db99 __exceptionPreprocess + 185
	1   libobjc.A.dylib                     0x0279d40e objc_exception_throw + 47
	2   CoreFoundation                      0x0264dad1 -[NSException raise] + 17
	3   Foundation                          0x0008a0f3 _NSSetUsingKeyValueSetter + 135
	4   Foundation                          0x0008a061 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 285
	5   UIKit                               0x0050870a -[UIRuntimeOutletConnection connect] + 112
	6   CoreFoundation                      0x025c3d0f -[NSArray makeObjectsPerformSelector:] + 239
	7   UIKit                               0x00507121 -[UINib instantiateWithOwner:options:] + 1041
	8   UIKit                               0x00508eb5 -[NSBundle(UINSBundleAdditions) loadNibNamed:owner:options:] + 168
	9   UIKit                               0x003be95f -[UIViewController _loadViewFromNibNamed:bundle:] + 70
	10  UIKit                               0x003bc675 -[UIViewController loadView] + 120
	11  UIKit                               0x003bc54f -[UIViewController view] + 56
	12  UIKit                               0x003ba9f4 -[UIViewController contentScrollView] + 42
	13  UIKit                               0x003ca7e2 -[UINavigationController _computeAndApplyScrollContentInsetDeltaForViewController:] + 48
	14  UIKit                               0x003c8ea3 -[UINavigationController _layoutViewController:] + 43
	15  UIKit                               0x003ca067 -[UINavigationController _startTransition:fromViewController:toViewController:] + 326
	16  UIKit                               0x003c4ccd -[UINavigationController _startDeferredTransitionIfNeeded] + 266
	17  UIKit                               0x003cbd8b -[UINavigationController pushViewController:transition:forceImmediate:] + 876
	18  UIKit                               0x003c4b67 -[UINavigationController pushViewController:animated:] + 62
	19  Universiade 2011                    0x0002a82e -[MoreTableView tableView:didSelectRowAtIndexPath:] + 1050
	20  UIKit                               0x00385a48 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
	21  UIKit                               0x0037c32e -[UITableView _userSelectRowAtIndexPath:] + 219
	22  Foundation                          0x0009121a __NSFireDelayedPerform + 441
	23  CoreFoundation                      0x0262ef73 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
	24  CoreFoundation                      0x026305b4 __CFRunLoopDoTimer + 1364
	25  CoreFoundation                      0x0258cdd9 __CFRunLoopRun + 1817
	26  CoreFoundation                      0x0258c350 CFRunLoopRunSpecific + 208
	27  CoreFoundation                      0x0258c271 CFRunLoopRunInMode + 97
	28  GraphicsServices                    0x02f2c00c GSEventRunModal + 217
	29  GraphicsServices                    0x02f2c0d1 GSEventRun + 115
	30  UIKit                               0x00320af2 UIApplicationMain + 1160
	31  Universiade 2011                    0x00002a5c main + 102
	32  Universiade 2011                    0x000029ed start + 53
)
terminate called after throwing an instance of 'NSException'
Program received signal:  “SIGABRT”.

 

5.比较恼火的BAD_ACCESS,网上有个做法是设置ZomebieEnabled,确实不错,有的时候确不显示(不知道是不是人品问题..)不显示的时候用俺刚才说的debuger左上角的定位至离错误最近的地方那个方法,一般都OK了

 

  吾生有涯,debug无涯,未完待续

 

  • 大小: 146.1 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics