iOS开发

iOS-为系统控件添加属性

2019-05-29  本文已影响0人  拎着猫走的鱼

例如为UIImage添加一个照片No

#import <UIKit/UIKit.h>

@interface UIImage (PhotoNo)

@property(nonatomic,copy) NSString *photoNo;

@end

#import "UIImage+PhotoNo.h"
#import <objc/runtime.h>

static NSString *photoNoKey = @"photoNoKey";

@implementation UIImage (PhotoNo)

- (void)setPhotoNo:(NSString *)photoNo {
    objc_setAssociatedObject(self, &photoNoKey, photoNo, OBJC_ASSOCIATION_COPY);
}
- (NSString *)photoNo {
    return objc_getAssociatedObject(self, &photoNoKey);
    
}

@end
UIImage *image = [UIImage imageNamed:@"test.png"];
image.photoNo = @"test123";
上一篇下一篇

猜你喜欢

热点阅读