What's the point of NSAssert, actually?

2020-01-30 02:42发布

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 of it? And is it right to put an NSAssert just above any assumption I make in code, like a function that should never receive a -1 as param but may a -0.9 or -1.1?

10条回答
倾城 Initia
2楼-- · 2020-01-30 03:12

NSAssert gives you more than just crashing the app. It tells you the class, method, and the line where the assertion occurred. All the assertions can also be easily deactivated using NS_BLOCK_ASSERTIONS. Thus making it more suitable for debugging. On the other hand, throwing an NSException only crashes the app. It also does not tell about the location of the exception, nor can it be disabled so simply. See the difference in the images below.

The app crashes because an assertion also raises an exception, as the NSAssert documentation states:

When invoked, an assertion handler prints an error message that includes the method and class names (or the function name). It then raises an NSInternalInconsistencyException exception.

NSAssert:

Logs after an assertion

NSException:

Logs after an exception

查看更多
Melony?
3楼-- · 2020-01-30 03:16

Apart from what everyone said above, the default behaviour of NSAssert() (unlike C’s assert()) is to throw an exception, which you can catch and handle. For instance, Xcode does this.

查看更多
女痞
4楼-- · 2020-01-30 03:17

I can't really speak to NSAssert, but I imagine that it works similarly to C's assert().

assert() is used to enforce a semantic contract in your code. What does that mean, you ask?

Well, it's like you said: if you have a function that should never receive a -1, you can have assert() enforce that:

void gimme_positive_ints(int i) {
  assert(i > 0);
}

And now you'll see something like this in the error log (or STDERR):

Assertion i > 0 failed: file example.c, line 2

So not only does it safe-guard against potentially bad inputs but it logs them in a useful, standard way.

Oh, and at least in C assert() was a macro, so you could redefine assert() as a no-op in your release code. I don't know if that's the case with NSAssert (or even assert() any more), but it was pretty useful to compile out those checks.

查看更多
等我变得足够好
5楼-- · 2020-01-30 03:18

NSAssert (and its stdlib equivalent assert) are to detect programming errors during development. You should never have an assertion that fails in a production (released) application. So you might assert that you never pass a negative number to a method that requires a positive argument. If the assertion ever fails during testing, you have a bug. If, however, the value that's passed is entered by the user, you need to do proper validation of the input rather than relying on the assertion in production (you can set a #define for release builds that disables NSAssert*.

查看更多
登录 后发表回答