• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
demoFootman
博客园    首页    新随笔    联系   管理    订阅  订阅

runTime动态给类添加属性

#项目中需要给系统类添加属性

#需要注意的地方就是.m中   set 和 get  ,get方法中方法名和添加的属性名一致,set中可以用驼峰

 

#import <UIKit/UIKit.h>


@interface UITextField (AddProperty)
/** 添加额外标签*/
@property (nonatomic, copy)  NSString* otherTag;
@end
//
//  UITextField+AddProperty.m
//  phone-wisdomFarmland
//
//  Created by apple on 2019/2/19.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "UITextField+AddProperty.h"
#import <objc/runtime.h>

static NSString *targetVaulekey = @"test_otherTagKey";

@implementation UITextField (AddProperty)
@dynamic otherTag;

-(NSString *)otherTag{
    return objc_getAssociatedObject(self, &targetVaulekey);
}

- (void)setOtherTag:(NSString *)otherTag{
    objc_setAssociatedObject(self, &targetVaulekey, otherTag, OBJC_ASSOCIATION_RETAIN);
}

@end

 

这不是开往幼儿园的车~
posted @ 2019-02-22 10:46  demoFootman  阅读(178)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3