كيف هو ObjC شبيبة الجسر يترجم ؟
-
21-12-2019 - |
سؤال
كيف JavaScriptCore إطار ترجمة جافا سكريبت الكائنات إلى كائنات الهدف-C ، والعكس بالعكس ؟ هل الجسر استخدام نفس الثنائية بروتوكول التواصل لكل من الهدف اللغات ؟
المحلول
الهدف-ج إطار قدم مع iOS7 يفعل كل رفع الأحمال الثقيلة بالنسبة لك.أنت لا تحتاج إلى التفكير من الكائنات نفس الثنائية ولكن تفسيرها بشكل مختلف - إطار ينفذ نسخ عند التحويل بين ObjC و جافا سكريبت.
من 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 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;
لكل خاصية أعلن في البروتوكول في إطار بناء شبيبة جالبة/واضعي حتى داخل جافا سكريبت يمكنك القيام به فقط:
myJSPointVar.x = 10;
بالنسبة لأولئك القراء يبحث عن مقدمة إلى JavaScriptCore إطار التحقق من WWDC 2013 فيديو/pdf "إدماج جافا سكريبت في تطبيقات" الدورة على أبل المطور الشبكة: https://developer.apple.com/videos/wwdc/2013/?id=615
نصائح أخرى
إلقاء نظرة على هذه الفئة: