Objective C Chaining calls

Source: Internet
Author: User

Cause

When used on a certain day DKChainableAnimationKit , you can write the following code:

view.animation.rotate(180).anchorTopLeft.thenAfter(1.0).rotate(90).anchorCenter.animanimation

Coincidentally. Masonryactually, it's the same thing.

make.right.equalTo(self.view).with.offset(-10);
Principle

Look at the code, in fact, through a realization of all methods Chaining Method Object , each method will return a ' Block
, 这个返回类型为the parameters for block Chaining Method Object block are the type of arguments you want to pass in.

@interfaceChainingmethodobject:nsobject-(Chainingmethodobject * (^) (void)) doA;-( Chainingmethodobject * (^) (nsinteger i)) dob;-(Chainingmethodobject * (^) (nsstring* str") doc;-(Chainingmethodobject * (^) (nsstring* str, nsarray* Array)) DoD;  @end  @implementation chainingmethodobject-(Chainingmethodobject * (^) (NSInteger i)) doB{return ^id (NSInteger i) {//do actual stuff related with B return self;}; ... Other methods like  @end           

Usually, there ChainingMethodObject will be, delegate depending on the actual use of the situation, such as the animation library DKChainableAnimationKit , animation there is a weak var view:UIView pointer to the UIView so that the target view operation.

@implementation ChainingMethodObject- (id) initWithObject:(id)obj{ self = [super init]; _delegate = obj; return self;}@end
@interface HostObject()ChainingMethodObject * _cObj;@end@implementation HostObject (ChainingMethodObject)- (ChainingMethodObject *) getChainingMethodObject{ if (!_cObj) _cObj = [[ChainingMethodObject alloc] initWithObject:self]; return _cObj;}@end

Then you can:

HostObject* hostObject = [HostObject new];[hostOjbect getChainingMethodObject].doA.doC(@"Hi there!").doD(@"Hello",@[@1,@2]).doB(100).doA;
Reference

Dkchainableanimationkit

Masonry

Objective C Chaining calls

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.