本文介绍了将Objective-C的异常提升到Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JavascriptCore允许我们通过JSExport协议将我们的Objective-C类暴露给javascript。假设在一个暴露的Objective-C类的方法中,遇到引发异常有意义的条件。有没有办法创建所述异常并提高它,以便JavaScript代码可以处理它,因为它会处理一个常规的JavaScript异常?



我试着做一个常规的旧的 [NSException raise ...] ,并立即导致崩溃。 p>

我被降级为检测提升条件,将该条件恢复为javascript,并让JavaScript执行抛出?

解决方案

Objective-C JSContext 对象具有异常属性,可用于向JavaScript抛出异常。根据头文件中的注释:


JavascriptCore permits us to expose our Objective-C classes to javascript via the JSExport protocol. Let's say, in one of the exposed Objective-C class's methods, a condition is encountered where it makes sense to raise an exception. Is there a way to create said exception and raise it such that the javascript code can process it as it would process a regular javascript exception?

I tried doing a regular old [NSException raise...] and that immediately caused a crash.

Am I relegated to detecting the raise condition, passing that condition back to javascript, and having the javascript do the throw?

解决方案

Objective-C JSContext object have exception property, that may be used to throw an exception to JavaScript. According comments in header file:

这篇关于将Objective-C的异常提升到Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 20:38