最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

swift - In what scenarios would iOS JSContext be nil since it returns an optional type? - Stack Overflow

programmeradmin1浏览0评论

I have an app which relies on Apple's JavaScriptCore heavily.

When I do:

let context = JSContext()

the context is an optional type.

In what scenarios would it be nil? I need to know how to properly handle this or which devices I should not support.

I have an app which relies on Apple's JavaScriptCore heavily.

When I do:

let context = JSContext()

the context is an optional type.

In what scenarios would it be nil? I need to know how to properly handle this or which devices I should not support.

Share Improve this question asked Mar 4 at 21:17 sudoExclamationExclamationsudoExclamationExclamation 8,82610 gold badges51 silver badges120 bronze badges 4
  • The documentation says that it returns an implicitly unwrapped optional, so that would imply that context won't be nil – Paulw11 Commented Mar 4 at 21:28
  • @Paulw11 While I also believe that is the case, the documentation saying that is just because the objective-c header is not annotated with nullability annotations. It doesn't imply that the init will never return nil. – Sweeper Commented Mar 4 at 21:40
  • I guess. However, since the ! and the optional return is simply because there is no nullability and the documentation doesn't state that the initialiser can fail it would seem that the initialiser probably isn't, in fact, failable. . Given that the OP's app is heavily dependent on Javascript probably their only reasonable response if they do get nil at runtime is to either post some sort of error message and crash when the user acknowledges it or just crash. – Paulw11 Commented Mar 4 at 21:43
  • Further, if you look at the Objective-C version of the documentation there is no indication that it will return nil, so I think it is safe to assume that the optionality on the Swift side is just an artefact of an incomplete header implementation. – Paulw11 Commented Mar 4 at 21:47
Add a comment  | 

1 Answer 1

Reset to default 2

The Objective-C initialiser is imported into Swift as init!() - an initialiser that returns an implicitly-unwrapped optional (IUO). When you declare a context with this as the initialiser expression, the type of context will be inferred to be an optional type. The idea is that IUOs gets turned into regular optionals whenever possible (see SE-0054).

You can get a non-optional JSContext just by adding an explicit type annotation:

let context: JSContext = JSContext()

The reason why the initialiser is imported as init!() and not init() or init?(), is likely because the Objective-C header file does not have nullability annotations. That is, the initialiser is declared:

- (id)init;

instead of either of these

- (nullable id)init;
- (nonnull id)init;

This can be seen all over JavaScriptCore. All the parameter types and return value types are all imported into Swift as IUOs.

All this is to say, that it could very well be the case that init can never return nil - it's just not annotated properly so it is not imported as a init() into Swift.

Judging from the rest of the documentation in JavaScriptCore, it is highly likely that JSContext.init will not return nil. In the documentation of many other methods and properties in the module, it is clearly documented when something returns nil if it does. The documentation for JSContext.init does not mention this.

发布评论

评论列表(0)

  1. 暂无评论