IOS extension mechanism category and associative

Source: Internet
Author: User

When writing Sina Weibo, I want to solve the problem of clicking Weibo image enlargement. Here I am using the category and associative extension mechanisms to add a method and an attribute to the uiimageview extension, this method is used to enlarge the clicked image, and this attribute is the URL of the download link of the image.


The two extension mechanisms are described below:

Category and associative are two features of the objective-C extension mechanism. category can be used to expand the method; associative can use it to expand attributes.

During iOS development, the former category is more common and simple. Here we will not talk about it. Here we will mainly talk about associative;

The latter associative is less commonly used. To use associative, you must use # Import
<Objc/runtime. h>, and then call the objc_setassociatedobject and objc_getassociatedobject methods to add the setter and getter methods to the attributes respectively to implement attribute extension.


The following two methods are described:

①: Void objc_setassociatedobject (ID object, void * Key, id value, objc_associationpolicy Policy)

The parameters are as follows:

Parameters

Object: the source object for the association.

Key: the key for the association.

Value: the value to associate with the key for object. Pass nil to clear an existing association.

Policy: the Policy for the Association

The policy includes

Enum {

Objc_association_assign = 0,

Objc_association_retain_nonatomic = 1,

Objc_association_copy_nonatomic = 3,

Objc_association_retain = 01401,

Objc_association_copy = 01403

};

②: Id objc_getassociatedobject (ID object, void * key)

Parameters

Object: the source object for the association.

Key: the key for the association.

Return Value

The value associated with the key for object.

All of them are relatively simple. Here is a demo to describe them!

Here I want to extend uiimageview to add a method and an attribute for it.

The header file of category:

#import <UIKit/UIKit.h>@interface UIImageView (associate)@property(nonatomic,strong)NSString* myString;-(void)Output;@end

CATEGORY implementation file:

#import <objc/runtime.h>#import "UIImageView+associate.h"static void * MyKey = (void *)@"MyKey";@implementation UIImageView (associate)-(NSString*)myString {    return objc_getAssociatedObject(self, MyKey);}-(void)setMyString:(NSString *)string {    objc_setAssociatedObject(self, MyKey, string, OBJC_ASSOCIATION_COPY_NONATOMIC);}-(void)Output {    NSLog(@"output mystring:%@",self.myString);}@end

Note: An Attribute and a method are added to the header file, and the setter and getter methods are rewritten for the attribute using the associative feature in the implementation file, which is relatively simple.

Test:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Icon@2x.png"]];    imageView.bounds = CGRectMake(50, 50, 100, 100);    imageView.myString = @"hello world";    [self.view addSubview:imageView];        [imageView Output];

After running, an image is displayed on the simulator. The terminal output is output mystring: Hello world.

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.