将Objective-C的异常提升到Javascript [英] Raising an Exception from Objective-C back to Javascript

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

问题描述

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



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

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

解决方案

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


在从JavaScript到Objective-C块或
方法回调之前,先前异常属性的值将被保留
,属性将被设置为nil。回调完成
后,将读取异常属性的新值,并恢复先前值
。如果异常的新值不为零,则回调将
导致该值被抛出。



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:

Before a callback is made from JavaScript to an Objective-C block or method, the prior value of the exception property will be preserved and the property will be set to nil. After the callback has completed the new value of the exception property will be read, and prior value restored. If the new value of exception is not nil, the callback will result in that value being thrown.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆