What's the point of NSAssert, actually?

后端 未结 10 2063
一个人的身影
一个人的身影 2020-12-04 05:43

I have to ask this, because: The only thing I recognize is, that if the assertion fails, the app crashes. Is that the reason why to use NSAssert? Or what else is the benefit

10条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-04 06:07

    NSAssert make app crash when it match with the condition. If not match with the condition the next statements will execute. Look for the EX below:

    I just create an app to test what is the task of NSAssert is:

        - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
        [self testingFunction:2];
    }
    
    -(void)testingFunction: (int)anNum{
        // if anNum < 2 -> the app will crash
        // and the NSLog statement will not execute
        // that mean you cannot see the string: "This statement will execute when anNum < 2"
        // into the log console window of Xcode
        NSAssert(anNum >= 2, @"number you enter less than 2");
        // If anNum >= 2 -> the app will not crash and the below 
        // statement will execute
        NSLog(@"This statement will execute when anNum < 2");
    }
    

    into my code the app will not crash.And the test case is:

    • anNum >= 2 -> The app will not crash and you can see the log string:"This statement will execute when anNum < 2" into the outPut log console window
    • anNum < 2 -> The app will crash and you can not see the log string:"This statement will execute when anNum < 2"

提交回复
热议问题