【问题标题】:How do we know JSContext in Objective-C can do subscripting?我们如何知道 Objective-C 中的 JSContext 可以做下标?
【发布时间】:2021-04-30 07:12:04
【问题描述】:

有很多这样的示例代码:

JSContext *context = [[JSContext alloc] init];
        context[@"globalFunc"] =  ^() {
        NSArray *args = [JSContext currentArguments];
        for (id obj in args) {
            NSLog(@"got:%@", obj);
        }
    };

让我困惑的是context[@"globalFunc"] = *** 声明。好像 JSContext 支持下标(运算符 [ ])?

但是我没有找到任何官方文件这么说?那么我们怎么知道我们可以在 JSContext 中下标来设置/获取对象呢?

【问题讨论】:

    标签: objective-c webkit javascriptcore


    【解决方案1】:

    好的,我错过了JSContext声明后的扩展代码-_-

    @interface JSContext (SubscriptSupport)
    
    /*!
    @method
    @abstract Get a particular property on the global object.
    @result The JSValue for the global object's property.
    */
    - (JSValue *)objectForKeyedSubscript:(id)key;
    
    /*!
    @method
    @abstract Set a particular property on the global object.
    */
    - (void)setObject:(id)object forKeyedSubscript:(NSObject <NSCopying> *)key;
    
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-27
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 1970-01-01
      相关资源
      最近更新 更多