简体   繁体   English

NSAssert在uiwebview委托方法中不起作用

[英]NSAssert not work in uiwebview delegate method

In a UIWebView's delegate method webView:shouldStartLoadWithRequest:navigationType: , I put an NSAssert there, but it just output a log, instead of terminating. 在UIWebView的委托方法webView:shouldStartLoadWithRequest:navigationType: ,我在此处放置了一个NSAssert ,但它仅输出日志,而不是终止日志。 Here is my code: 这是我的代码:

- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
            navigationType:(UIWebViewNavigationType)navigationType
{
    NSAssert(NO,@"assertion in delegate");
    return YES;
}

and the output: 和输出:

*** WebKit discarded an uncaught exception in the webView:decidePolicyForNavigationAction:request:frame:decisionListener: delegate: assertion in delegate *** WebKit丢弃了webView:decidePolicyForNavigationAction:request:frame:decisionListener中的未捕获异常:委托:委托中的断言

Failed NSAssert s raise ObjC exceptions. 失败的NSAssert引发ObjC异常。 ( NSInternalInconcistencyException to be precise.) Anyone can install exception handlers or other mechanisms to define what happens to exceptions that are raised in code that they call. NSInternalInconcistencyException ,是NSInternalInconcistencyException 。)任何人都可以安装异常处理程序或其他机制来定义在他们调用的代码中引发的异常的处理方式 And those mechanisms don't have to include halting the process (though continuing after an exception is generally not a great idea). 而且这些机制不必包括停止过程(尽管在例外之后继续执行通常不是一个好主意)。

When you raise ObjC exceptions in a callback, you're not guaranteed that execution will terminate as a result — you're at the mercy of whatever exception handling was set up by the code that called you. 当您在回调中引发ObjC异常时,您不能保证执行会因此而终止-您不受任何调用您的代码设置的异常处理的约束。 If you want to bring the whole process crashing down due to some failure in delegate code, it's probably best to abort() it yourself. 如果您要使整个过程由于委托代码中的某些失败而崩溃,那么最好自己abort()

NSAssert raises an Objective-C exception and these can be caught, so it doesn't guarantee your program will be aborted. NSAssert引发一个Objective-C异常,并且可以捕获这些异常,因此不能保证您的程序将被中止。 Using it in your own code is generally fine, but if your code is called by a framework - such as when a delegate is invoked - it depends on what the framework does. 在您自己的代码中使用它通常是可以的,但是如果您的代码是由框架调用的(例如调用委托时),则取决于框架的功能。 As you have discover WebKit catches exceptions and discards or handles them itself. 如您所知,WebKit会捕获异常并自行丢弃或处理它们。

The simple solution is to use the standard assert() function . 简单的解决方案是使用标准的assert()函数 This takes a single Boolean expression and will abort the program printing out the expression, file name and line number of the assertion. 这将使用单个布尔表达式,并且将中止程序,并打印出声明的表达式,文件名和行号。 This function does not use Objective-C exceptions, it uses the standard abort() function , and so cannot be caught. 该函数不使用Objective-C异常,它使用标准的abort()函数 ,因此无法捕获。

HTH 高温超导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM