IOS: Write a chained program with block

Source: Internet
Author: User

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

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.