iosobjective-ciphonemacoscocoa

Unconditional NSAssert in Objective-C?


Sometimes I like to put NSAssert(NO, @"this should never happen") in a block of code that should never be reached. Is there a better alternative for such unconditional asserts in Objective-C? I would like something like NSTerminate(@"this should never happen") - a convenient function which prints a message and terminates the program unconditionally.


Solution

  • IMO, I think NSAssert is appropriate because it is only in Development (by default). You do not want an unexpected crash in Production if the block of code changes in the near future somehow, do you?

    However, you can take a look at NSException raise here:

    [NSException raise: @"InvalidBlock" fromat: @"This should never happen"];