ObjC JS桥如何翻译? [英] How is an ObjC JS bridge translated?
问题描述
JavaScriptCore框架如何将JavaScript对象转换为Objective-C对象,反之亦然?网桥是否使用相同的二进制协议为两种目标语言进行通信?
How does the JavaScriptCore framework translate JavaScript objects into Objective-C objects, and vice versa? Does the bridge use the same binary protocol to communicate for both of the target languages?
推荐答案
iOS7引入的Objective-C框架为您完成了所有繁重的工作. 您无需将对象视为相同的二进制文件,但可以将它们理解为不同的对象-在ObjC和Javascript之间进行转换时,框架执行复制.
The Objective-C framework introduced with iOS7 does all the heavy lifting for you. You don't need to think of the objects as the same binary but interpreted differently - the framework performs a copy when converting between ObjC and Javascript.
来自JSValue.h:
From 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.
例如(对于简单类型):
For example (for simple types):
NSString *myString = [javascriptContext[@"myJSVar"] toString];
和
javascriptContext[@"myJSVar"] = myString;
或更复杂的对象,请使用JSExport协议:
or for a more complex object, use the 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;
对于协议中声明的每个属性,框架都会构建JS getter/setter,因此您可以在Javascript中执行以下操作:
For each property declared in the protocol, the framework will build JS getter/setters so inside Javascript you can just do:
myJSPointVar.x = 10;
对于那些希望了解JavaScriptCore框架的读者,请查看Apple开发者网络上的2013 WWDC视频/pdf将JavaScript集成到本机应用程序"会议:https://developer.apple.com/videos/wwdc/2013/?id=615
For those readers looking for an intro to the JavaScriptCore framework, check out the 2013 WWDC video/pdf "Integrating JavaScript into Native Apps" session on Apple's developer network: https://developer.apple.com/videos/wwdc/2013/?id=615
这篇关于ObjC JS桥如何翻译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!