gpt4 book ai didi

ios - 将异常从 Objective-C 引发回 Javascript

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:00:16 24 4
gpt4 key购买 nike

JavascriptCore 允许我们通过 JSExport 协议(protocol)将我们的 Objective-C 类暴露给 javascript。比方说,在公开的 Objective-C 类的方法之一中,遇到了引发异常的情况。有没有办法创建所述异常并引发它,以便 javascript 代码可以像处理常规 javascript 异常一样处理它?

我尝试执行常规的旧 [NSException raise...] 并立即导致崩溃。

我是否只能检测引发条件,将该条件传回 javascript,然后让 javascript 执行抛出?

最佳答案

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

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.

关于ios - 将异常从 Objective-C 引发回 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29979041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com