First, Introduction
Chain programming is a relatively new way of programming, simple and intuitive, it is more comfortable to use. At present, the more famous mansory and Babybluetooth are the third-party frameworks written using chained programming.
Second, the wording
The chain programming differs from the traditional way, the first writing is more awkward, but once used, it is more handy. So, how to write it? It is actually an object that invokes its instance method and returns a block (already defined), and the object is returned inside the block. And so on, the object does not argue with how many of its instance methods will eventually return the object itself.
Third, examples
Learnmanager class definition and implementation
////LearnManager.h//chainprogramming////Created by Xiahuan on 2017/9/30.//Copyright 2017 The summer is far from full. All rights reserved.#import<Foundation/Foundation.h>/** Define block, return value type is Learnmanager object*/@classlearnmanager;typedef Learnmanager* (^learnchineseblock) (NSString *chinesename); typedef learnmanager* (^learnenglishblock) (NSString *englishname); typedef learnmanager* (^learnfranchblock) (NSString *franchname);@interfaceLearnmanager:nsobject/** Defines the object method @return The return value is a defined block*/-(Learnchineseblock) Learnchinese;-(Learnenglishblock) learnenglish;-(Learnfranchblock) learnfranch;@end
////LEARNMANAGER.M//chainprogramming//Created by Xiahuan on 2017/9/30.//Copyright 2017 The summer is far from full. All rights reserved.#import "LearnManager.h"@implementationLearnmanager-(learnchineseblock) learnchinese{//External returns a block return^learnmanager * (NSString *chinesename) {NSLog (@"%@", [NSString stringWithFormat:@"Learning%@! ", Chinesename]); //internal Block Returns a Learnmanager object returnSelf ; };}-(learnenglishblock) learnenglish{//External returns a block return^learnmanager * (NSString *englishname) {NSLog (@"%@", [NSString stringWithFormat:@"Learning%@! ", Englishname]); //internal Block Returns a Learnmanager object returnSelf ; };}-(learnfranchblock) learnfranch{//External returns a block return^learnmanager * (NSString *franchname) {NSLog (@"%@", [NSString stringWithFormat:@"Learning%@! ", Franchname]); //internal Block Returns a Learnmanager object returnSelf ; };}@end
The Viewcontroller class imports and calls
////VIEWCONTROLLER.M//chainprogramming////Created by Xiahuan on 2017/9/30.//Copyright 2017 The summer is far from full. All rights reserved.//#import "ViewController.h"#import "LearnManager.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; Learnmanager*learn =[[Learnmanager alloc] init];
The //learn object first calls the method to return a block, then passes the string parameter to the block and invokes it, and returns the object learn itself Learn.learnchinese (@"language"). Learnenglish (@"English"). Learnfranch (@"French");}@end
Four, print
.- the- - -: A:10.288chainprogramming[92884:3768713] Learn Chinese! .- the- - -: A:10.288chainprogramming[92884:3768713] Learn English! .- the- - -: A:10.289chainprogramming[92884:3768713] Learn French!
IOS: Write a chained program with block