Add attributes to categories (implemented via @dynamic and Objc_setassociatedobject)

Source: Internet
Author: User

by @dynamic and Objc_setassociatedobject, you can add attributes to a category, which explains: Programmers manually implement setters and getters, while the latter dynamically add properties to objects. Two mates can be used to add properties to an object.

The following code takes ScrollView as an example, adds a Refreshview property to ScrollView, and other places can use Refreshview like normal properties:

"Header File"

@interface Uiscrollview (Refresh) @property (nonatomic) refreshview * Refreshview; @end

"Source file" requires runtime header file

#import <objc/runtime.h>static Char Krefreshview, @implementation Uiscrollview (Refresh) @dynamic refreshview;-( void) Setrefreshview: (Refreshview *) Aview {        objc_setassociatedobject (self, &krefreshview, Aview, OBJC_ association_retain_nonatomic);} -(Refreshview *) Refreshview {        return objc_getassociatedobject (self, &krefreshview);} @end


Add attributes to categories (implemented via @dynamic and Objc_setassociatedobject)

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.