• NSAssert()是一个宏,用于开发阶段调试程序中的Bug,通过为NSAssert()传递条件表达式来断定是否属于Bug,满足条件返回真值,程序继续运行,如果返回假值,则抛出异常,并切可以自定义异常描述。NSAssert()是这样定义的:
    • #define NSAssert(condition, desc)
    • condition是条件表达式,值为YES或NO;desc为异常描述,通常为NSString。当conditon为YES时程序继续运行,为NO时,则抛出带有desc描述的异常信息。NSAssert()可以出现在程序的任何一个位置。
  • 示例:
    • NSArray *arr = @[@"1",@"2"];
      NSAssert(arr != nil, @" arr is nil");
      NSLog(@"23456”);  //正常打印 23456
    • int a = 1;
      NSAssert(a != 1, @"a != 1");
      NSLog(@"23456”); //抛出异常a != 1
    • *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'a != 1'
  • NSParameterAssert()NSParameterAssert()只需要一个参数,如果参数存在程序继续执行,如果参数为空,则程序停止打印
    • int a = 1;
      NSParameterAssert(a);
      NSLog(@"23456");
posted on 2016-03-25 10:15  怡情_老二  阅读(367)  评论(0编辑  收藏  举报