Sometimes you need to constantly call the method, if you use the traditional scheme, you need to get the object multiple calls, such as a ball object, the implementation of up, down, left, right four methods, respectively, control the direction of the sphere, if you want to achieve the ball to the right and down, you need to write:
[Ball right]; [Ball down];
It would be more convenient if it was written as follows.
[[Ball right] down];
It is very simple to implement such a function, as long as each method returns self, but if the number of calls is many, there will be many brackets. It would be better if it could be changed to the back.
Ball.right (). Down ();
To implement such a call method, you can use the struct, let the method return a struct, this time plus (), equivalent to execute a struct, for example, there is a struct named Block1, no parameters, call method is Block1 (), so we write Ball.right () Equivalent to getting the block to be returned for execution. It should be noted that, in order to be able to execute the method through the point syntax, the method can not have parameters, otherwise it can only be used in brackets, parameters to pass, through the block can be.
The following code implements four non-parametric methods and an argument method:
#import <Foundation/Foundation.h> @interface ball:nsobject-(Ball * (^) ()) up;-(Ball * (^) ()) down;-(Ball * (^) ()) right;-(Ball * (^) ()) left;-(Ball * (^) (nsstring *sth)) dosomething; @end
#import "Ball.h" @implementation ball-(Ball * (^) ()) up{ return ^{ NSLog (@ "up"); return self; }; } -(Ball * (^) ()) down{ return ^{ NSLog (@ "down"); return self; }; } -(Ball * (^) ()) right{ return ^{ NSLog (@ "left"); return self; };} -(Ball * (^) ()) left{ return ^{ NSLog (@ "right"); return self; }; } -(Ball * (^) (NSString *)) dosomething{ return ^ (NSString *sth) { NSLog (@ ' Do <%@> ', STH); return self; }; } @end
Pay attention to the structure of the writing can, each method within the direct return to the structure, the need to do things directly in the structure of the body to complete.
To test the chained invocation of this class:
#import <Foundation/Foundation.h> #import "Ball.h" int main (int argc, const char * argv[]) { @autoreleasepool { C3/>ball *b = [[Ball alloc] init]; B.up (). Down (). Right (). dosomething (@ "rotate"). Left (); Method cannot have parameters, otherwise only the method can be called with brackets. The //parameter returns block via block receive //b.up, () means call block, from here can pass in parameter } return 0;
Print out the data:
2015-08-11 22:16:23.777 chained programming [14240:145326] up2015-08-11 22:16:23.778 chained programming [14240:145326] down2015-08-11 22:16:23.778 Chained programming [14240:145326] left2015-08-11 22:16:23.779 chained programming [14240:145326] do <rotate>2015-08-11 22:16:23.779 chained programming [ 14240:145326] Right
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
(118) using Block for chain programming