Magic RAC macro, magic RAC macro

Source: Internet
Author: User

Magic RAC macro, magic RAC macro
First, let's talk about the macros that must be known in RAC.

  1. RAC (TARGET, [KEYPATH, [NIL_VALUE])
Usage:
RAC (self. outputLabel, text) = self. inputTextField. rac_textSignal; RAC (self. outputLabel, text, @ "show me when nil is received") = self. inputTextField. rac_textSignal;

This macro is the most commonly used. RAC () always appears on the left side of the equal sign, and a RACSignal on the right side of the equal sign. It indicates that an object's attributes are bound to a signal, every value (id type) generated by signal is automatically executed:

  1. [TARGET setValue: value? : NIL_VALUE forKeyPath: KEYPATH];
The numeric value is upgraded to NSNumber *. When setValue: forKeyPath is used, it is automatically downgraded to the basic type (int, float, BOOL, etc.). Therefore, it is no problem to bind a basic type value to RAC.
  1. RACObserve (TARGET, KEYPATH)
The function is to observe the KEYPATH attribute of the TARGET, which is equivalent to KVO, to generate the most common RACSignal usage, and bind the attribute with the RAC macro:
RAC(self.outputLabel, text) = RACObserve(self.model, name); 

 

The code above binds the label output to the name attribute of the model to achieve linkage. Any change to the name will make the label output
@weakify(Obj); @strongify(Obj); 

This macro is defined in RACEXTScope. h. RACFramework does not seem to be introduced by default and needs to be separately imported.

Their role is to manage references to self within the block:
@ Weakify (self); // defines the self_weak _ variable of _ weak [RACObserve (self, name) subscribeNext: ^ (NSString * name) {@ strongify (self ); // The local area defines a _ strong self pointer pointing to self_weak self. outputLabel. text = name;}];

 

Why is this macro so hanging? Adding @ in front of it is actually the @ in front of @ autoreleasepool {}, to be conspicuous. These two macros are set to appear in a pair, first weak and then strong

Related Article

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.