Tuesday, July 17, 2012

How and when to use NSAssert ?

If you want to write a code

1. Which is easier to debug(for you as well as others :P)
2. is a standard piece of code

Don't forget to include NSAssert in your code.

Here is an example of how you can use it:

//Assertion Code  
NSAssert((count <= 4)&&(count >= 3),@"colorString components count should be in range(3,5)"); 

Whenever the condition given above becomes false, it raises NSInternalInconsistencyException along with description message. 
Result in Console:

*** Assertion failure in +[ColorClass colorWithComponent:], /Users/mac/Desktop/ChartLib/Lib Files/Colors/ColorClass.m:66
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'colorString components count should be in range(3,5)'


Note:Please make sure that your XCode should be running in debug mode.

No comments:

Post a Comment