Nsassert () is just a macro that is used for bugs in the development phase of the debugger, by passing conditional expressions for Nsassert () to determine if a bug is true, to satisfy a conditional return truth, to run a program, to throw an exception if a false value is returned, and to customize the description of the exception. Nsassert () is defined in this way:
#define NSASSERT (condition, desc)
condition is a conditional expression, with a value of Yes or No;desc as an exception, usually nsstring. When the program continues to run when Conditon is yes, exception information with desc description is thrown when no. Nsassert () can appear at any location in the program. Specific examples are as follows:
When generating an Lotteryentry object, the incoming nsdate cannot be determined by nil, adding Nsassert (). Object initialization source code is as follows:
-(ID) initwithentrydate: (nsdate *) thedate{
Self =[super init];
if (self) {
Nsassert(thedate!= nil, @ "Argument must benon-nil");
entrydate =thedate;
Firstnumber = (int)random ()% + 1;
Secondnumber = (int)random ()% + 1;
}
return self;
}
Next, when you build the object, pass in a nsdate with a value of nil to see if the assertion is running.
Lotteryentry *nilentry =[[lotteryentry alloc] initwithentrydate:nil];
The assertion effect is as follows:
2013-01-17 20:49:12.486 lottery[3951:303] * * * terminating appdue to uncaught exception ' Nsinternalinconsistencyexception ', Reason: ' Argument must be Non-nil '
First throw Call stack:
(
0 corefoundation 0x00007fff90c590a6 __exceptionpreprocess +198
1 LIBOBJC. A.dylib 0x00007fff8fd2a3f0 Objc_exception_throw + 43
2 corefoundation 0x00007fff90c58ee8 +[nsexceptionraise:format:arguments:] + 104
3 Foundation 0x00007fff88dae6a2-[nsassertionhandlerhandlefailureinmethod:object:file:linenumber:des Cription:] +189
4 lottery 0x0000000100001929-[lotteryentryinitwithentrydate:] + 249
5 Lottery 0x0000000100001794 main + 932
6 libdyld.dylib 0x00007fff8d83f7e1 start + 0
)
Libc++abi.dylib:terminate called throwing an exception
"Go" Using assertion Nsassert () debugger Error