分享

iOS开发 NSAssert调试

 玄冰优 2015-03-05
首先看看NSAssert是怎么定义的:

NSAssert()只是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug,满足条件返回真值,程序继续运行,如果返回假值,则抛出异常,并切可以自定义异常描述。NSAssert()是这样定义的:

#define NSAssert(condition, desc)

condition是条件表达式,值为YES或NO;desc为异常描述,通常为NSString。当conditon为YES时程序继续运行,为NO时,则抛出带有desc描述的异常信息。NSAssert()可以出现在程序的任何一个位置。

去苹果文档中查询,得到如下信息:

#if !defined(_NSAssertBody)
#define NSAssert(condition, desc, ...)    \
    do {                \
    __PRAGMA_PUSH_NO_EXTRA_ARG_WARNINGS \
    if (!(condition)) {        \
        [[NSAssertionHandler currentHandler] handleFailureInMethod:_cmd \
        object:self file:[NSString stringWithUTF8String:__FILE__] \
            lineNumber:__LINE__ description:(desc), ##__VA_ARGS__]; \
    }                \
        __PRAGMA_POP_NO_EXTRA_ARG_WARNINGS \
    } while(0)
#endif

附上一个小例子
-(void)testForassert:(NSString *)str,
{
    if (str == nil) {
        NSAssert(NO, @"input----");
    }
}
如果你传的的确是nil
[self testForassert:nil];
如果 NSAssert(YES, @"input----");说明传的就是nil,你也正确的判断了,那么程序继续运行
但是 NSAssert(NO, @"input----");你判断为NO,意思是不是nil,那么会认为你的程序出问题了
看看打印结果,根据打印结果去调试程序,会快速不少
2015-03-05 11:34:05.579 TestForPocket[1089:50845] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'input----'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010950ff35 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x00000001091a8bb7 objc_exception_throw + 45
    2   CoreFoundation                      0x000000010950fd9a +[NSException raise:format:arguments:] + 106
    3   Foundation                          0x00000001078215df -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 195
    4   TestForPocket                       0x00000001076b7028 -[ViewController testForassert:] + 216
    5   TestForPocket                       0x00000001076b63a5 -[ViewController viewDidLoad] + 245
    6   UIKit                               0x0000000107f7da90 -[UIViewController loadViewIfRequired] + 738
    7   UIKit                               0x0000000107fac06b -[UINavigationController _layoutViewController:] + 44
    8   UIKit                               0x0000000107fac5b5 -[UINavigationController _updateScrollViewFromViewController:toViewController:] + 216
    9   UIKit                               0x0000000107fac6b4 -[UINavigationController _startTransition:fromViewController:toViewController:] + 92
    10  UIKit                               0x0000000107fad487 -[UINavigationController _startDeferredTransitionIfNeeded:] + 523
    11  UIKit                               0x0000000107fadf47 -[UINavigationController __viewWillLayoutSubviews] + 43
    12  UIKit                               0x00000001080f3509 -[UILayoutContainerView layoutSubviews] + 202
    13  UIKit                               0x0000000107ed1973 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 521
    14  QuartzCore                          0x000000010c935de8 -[CALayer layoutSublayers] + 150
    15  QuartzCore                          0x000000010c92aa0e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 380
    16  QuartzCore                          0x000000010c92a87e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    17  QuartzCore                          0x000000010c89863e _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 242
    18  QuartzCore                          0x000000010c89974a _ZN2CA11Transaction6commitEv + 390
    19  UIKit                               0x0000000107e5654d -[UIApplication _reportMainSceneUpdateFinished:] + 44
    20  UIKit                               0x0000000107e57238 -[UIApplication _runWithMainScene:transitionContext:completion:] + 2642
    21  UIKit                               0x0000000107e55bf2 -[UIApplication workspaceDidEndTransaction:] + 179
    22  FrontBoardServices                  0x000000010ba462a3 __31-[FBSSerialQueue performAsync:]_block_invoke + 16
    23  CoreFoundation                      0x000000010944553c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 12
    24  CoreFoundation                      0x000000010943b285 __CFRunLoopDoBlocks + 341
    25  CoreFoundation                      0x000000010943b045 __CFRunLoopRun + 2389
    26  CoreFoundation                      0x000000010943a486 CFRunLoopRunSpecific + 470
    27  UIKit                               0x0000000107e55669 -[UIApplication _run] + 413
    28  UIKit                               0x0000000107e58420 UIApplicationMain + 1282
    29  TestForPocket                       0x00000001076bb6f3 main + 115
    30  libdyld.dylib                       0x000000010a73e145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多