gpt4 book ai didi

javascriptcore - ObjC JS 桥是如何翻译的?

转载 作者:行者123 更新时间:2023-12-02 21:28:04 27 4
gpt4 key购买 nike

JavaScriptCore 框架如何将 JavaScript 对象转换为 Objective-C 对象,反之亦然?桥接器是否使用相同的二进制协议(protocol)来对两种目标语言进行通信?

最佳答案

iOS7 中引入的 Objective-C 框架会为您完成所有繁重的工作。您不需要将对象视为相同的二进制文件,但解释不同 - 框架在 ObjC 和 Javascript 之间转换时执行复制

来自 JSValue.h:

// Conversion between Objective-C and JavaScript types.
//
// When converting between JavaScript values and Objective-C objects a copy is
// performed. Values of types listed below are copied to the corresponding
// types on conversion in each direction. For NSDictionaries, entries in the
// dictionary that are keyed by strings are copied onto a JavaScript object.
// For dictionaries and arrays, conversion is recursive, with the same object
// conversion being applied to all entries in the collection.

Objective-C type | JavaScript type
--------------------+---------------------
nil | undefined
NSNull | null
NSString | string
NSNumber | number, boolean
NSDictionary | Object object
NSArray | Array object
NSDate | Date object
NSBlock * | Function object *
id ** | Wrapper object **
Class *** | Constructor object ***

* Instances of NSBlock with supported arguments types will be presented to
JavaScript as a callable Function object. For more information on supported
argument types see JSExport.h. If a JavaScript Function originating from an
Objective-C block is converted back to an Objective-C object the block will
be returned. All other JavaScript functions will be converted in the same
manner as a JavaScript object of type Object.

** For Objective-C instances that do not derive from the set of types listed
above, a wrapper object to provide a retaining handle to the Objective-C
instance from JavaScript. For more information on these wrapper objects, see
JSExport.h. When a JavaScript wrapper object is converted back to Objective-C
the Objective-C instance being retained by the wrapper is returned.

*** For Objective-C Class objects a constructor object containing exported
class methods will be returned. See JSExport.h for more information on
constructor objects.

例如(对于简单类型):

NSString *myString = [javascriptContext[@"myJSVar"] toString];

javascriptContext[@"myJSVar"] = myString;

或者对于更复杂的对象,请使用 JSExport 协议(protocol):

@protocol MyPointExports <JSExport>
@property double x;
@property double y;
@end

@interface MyPoint : NSObject <MyPointExports>
// Put methods and properties not visible to JavaScript code here.
@end

...

javascriptContext[@"MyPoint"] = [MyPoint class]; // Define the class in Javascript

然后

MyPoint *p = [javascriptContext[@"myJSPointVar"] toObject];

javascriptContext[@"myJSPointVar"] = p;

对于协议(protocol)中声明的每个属性,框架将构建 JS getter/setter,因此在 Javascript 中您可以执行以下操作:

myJSPointVar.x = 10;

对于那些想要了解 JavaScriptCore 框架的读者,请观看 Apple 开发者网络上的 2013 年 WWDC 视频/pdf“将 JavaScript 集成到 native 应用程序中” session :https://developer.apple.com/videos/wwdc/2013/?id=615

关于javascriptcore - ObjC JS 桥是如何翻译的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23047512/

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