Jspatch Frequently Asked Questions

Source: Internet
Author: User

Original address: https://github.com/bang590/JSPatch/wiki/JSPatch%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98%E8%A7%A3%E7%AD%94

I. Rules of block usage

1, in Jspatch to pass the block to objective-c, you need to use block(paramTypes, function) function encapsulation: Block's detailed use method, the encapsulated value is not a JS function, not directly on the JS call:

Block ("BOOLfunction (b) {})blk (//crash    

If you want to call on JS, call the pre-encapsulation function:

function (b) {};  Block ("BOOL", func)func (//it ' s OK     

2, not allowed in the JS block to pass in undefined the contained array or object. For example ["JSPatch", undefined] , an incoming array or {obj: undefined} an object that has no errors in JavaScript, but is not allowed in objective-c.

3, if the parameters of the block contains block,paramtypes need to write NSBlock * .

Block ("BOOL, Nsblock *function (blk (True)}})   

4, in the objective-c into the Jspatch block will be converted to function, if you need to transfer the block back to OC, still need to use block(paramTypes, function) encapsulation.

Objective-c code-(void) Excuteblock: (jsblock) block {    if (block) {        block (@ "Hello, Jspatch  "); }} -(Jsblock) returnblock {Jsblock block = ^ (NSLog@ "%@return block;}      
Jspatch script self   .  Returnblock () Self.  Excuteblock (block ("NSString *", blk))      

5. Temporarily does not support the transfer of blocks containing floating-point parameters to the OC end, such as the following conditions:

Objective-c Code  Void (^jsblock) (cgfloat FL);-(void) Doblock: (jsblock) blk {    blk (123.  456F); }}
Jspatch script self .  Doblock (block ("CGFloat",function (console).  The resulting result will not be the result you want}))       

6. When using self in block, you need to assign self to another variable before using block, then use this variable in block. Such as:

Jspatch script = self self  .  Requestwithcompletion (block ("BOOL, nserror*function (SLF.  Label (). SetText ("Complete")}))          
Second, in the Jspatch said nilAnd [NSNull null]and use other special types

1, used in Jspatch null or undefined to represent the objective-c, the use of the nil nsnull expression [NSNull null] .

2, in the Jspatch to determine whether the Objective-c object is empty, if(!obj){} the use of the form and can not use if(obj == null){} orif(obj == undefined){}

3, in the Jspatch use struct, selector usage can refer to here

Third, call contains id *Methods for type parameters

See basic usage-pass id* parameters

Iv. using the parent class method in Jspatch

Using the self.super() Call parent class method

DefineClass ("Jptableviewcontroller", {  viewdidload self.  Super (). viewdidload ();}})     
V. Methods of invoking multiple parameters

Jspatch is used in _ place of Objective-c : , the Objective-c method that calls multiple arguments needs to be as follows.

var error = require (' Nserror'). Errorwithdomain_code_userinfo ("Unknown_error",-1, NULL) 

If you call a method that contains an underscore, you need to use a double underscore. For example:

-(void) P_privatemethod;

This can be called in Jspatch

Self. P__privatemethod ()

Methods that contain double (or more) underscores are not supported, such as - (void)p__privateMethod; . However, if you want to get a property with double (or more) underscores (which is more common than a double-underline method), you can use the KVC valueForKey method. For example:

ID __privateobject;

This can be obtained in Jspatch

Self. Valueforkey ("__privateobject")  
Vi. the nsarray,nsstring,nsdictionary in Jspatch

In Jspatch, it is not possible to mix objective-c, and NSArray NSString NSDictionary JavaScript Array String Object .

objective-c-(Nsarray *) Returnnsarray {    return @[@ "objective-c@" Swift"];}  
var Nsarray=Self.returnnsarray () var array = [ "Objective-c"  "Swift //the two cannot be mixed, because they are different types. var wrongstr = nsarray[0] // Get an incorrect object, do not get the Nsarray/nsdictionar object by using the method of removing the tag. var rightstr = nsarray. Tojs () [0] //first unbox the returned object, then takes out the data or var rightstr = nsarray. Objectatindex (0) //use Objective-c method to get objects in Nsarray/nsdictionary   

Refer to the basic usage document for details

Vii. Constants and macros

The constants in the objective-c can not be used directly on the JS, it is possible to replace them with specific values directly on JS, or to redefine the global variables with the same name on JS:

JSvar uirectedgenone   0,    uirectedgetop    3  

OBJECTIVE-C macro also cannot be used directly on the JS, if the definition of the macro is a value, you can define the same global variable JS, if the defined macro is a program, or at the bottom to obtain the value, for example CGFLOAT_MIN , you can add an extension to provide support:

jpmacrosupport+ (void)main:(Jscontext *)context{context[@ "Cgfloat_min"] = ^return Cgfloat_min; }}@end       
Require (' Jpengine').  Addextensions ([' Jpmacrosupport'])cgfloat_min ();     
Viii. acquisition and assignment of property/private variables

See DefineClass working with Documents

Nine, class global variables

Currently defined in the class of static global variables can not be obtained on the JS, to get this variable in JS, you need to have a class method or instance method in OC it returns:

NSString *name;  jptestobject+ (nsstring *)return name; @end   
Jptestobject.  //Get global variable value
X. Swift

When using the defineClass() override Swift class, the class name should be 项目名.原类名 , for example, the project demo uses Swift to define the Viewcontroller class, which is written when JS overrides this class method:

defineClass(‘demo.ViewController‘, {})
Xi. variable Parameters

Jspatch does not support calls with variable-parameter methods, such as:

OC: [NSString stringwithformat:@ "%d%@", I, obj]; NSString. stringWithFormat ("%d%@//not working   

Because the Jspatch principle is to invoke the method dynamically through Nsinvocation, and Nsinvocation does not support mutable parameters, see the official documentation.

12. Other issues

Can not be used require(‘NSNumber‘).alloc() for reasons see here

Jspatch Frequently Asked Questions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.