What are assertions or NSAssert good for in practice?

后端 未结 4 1260
迷失自我
迷失自我 2021-01-30 15:17

I wonder if it\'s a good habit to use NSAssert all over the place? What would be the benefit of doing that? In which situations is it a good idea to use it?

4条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-30 15:32

    The major warning are side effects. If you write:

    NSAssert([self.navigationController popViewControllerAnimated:YES]!=nil,@"Something fails");
    

    popViewControllerAnimated: will be executed in the debug version, but not in the release version that strips NSAssert(). This means that your release version will behave different to the debug version.

    This problem disappears if you are careful enough:

    UIViewController* vc = [self.navigationController popViewControllerAnimated:YES]; 
    NSAssert(vc!=nil,@"Something fails");
    

提交回复
热议问题