iOS 类增加成员变量
来源:互联网 发布:encore软件 编辑:程序博客网 时间:2024/05/16 03:12
RT
// 增加成员变量#import <Foundation/Foundation.h>@interface NSObject (AddProperty)@property (nonatomic,strong) NSString *stringProperty;@property (nonatomic,assign) NSInteger integerProperty;@end
#import "NSObject+AddProperty.h"#import <objc/runtime.h>//objc_getAssociatedObject和objc_setAssociatedObject都需要指定一个固定的地址,这个固定的地址值用来表示属性的key,起到一个常量的作用。static const void *StringProperty = &StringProperty;static const void *IntegerProperty = &IntegerProperty;//static char IntegerProperty;@implementation NSObject (AddProperty)@dynamic stringProperty;//set-(void)setStringProperty:(NSString *)stringProperty{ //use that a static const as the key objc_setAssociatedObject(self, StringProperty, stringProperty, OBJC_ASSOCIATION_RETAIN_NONATOMIC); //use that property's selector as the key: //objc_setAssociatedObject(self, @selector(stringProperty), stringProperty, OBJC_ASSOCIATION_RETAIN_NONATOMIC);}//get-(NSString *)stringProperty{ return objc_getAssociatedObject(self, StringProperty);}//set-(void)setIntegerProperty:(NSInteger)integerProperty{ NSNumber *number = [[NSNumber alloc]initWithInteger:integerProperty]; objc_setAssociatedObject(self, IntegerProperty, number, OBJC_ASSOCIATION_ASSIGN);}//get-(NSInteger)integerProperty{ return [objc_getAssociatedObject(self, IntegerProperty) integerValue];}@end
// 获取成员变量列表
@interface NSObject (Property)-(NSDictionary *)propertyDictionary;+ (NSArray *)classPropertyList;@end
#import "NSObject+Property.h"#import <Foundation/Foundation.h>#import <objc/runtime.h>@implementation NSObject (Property)-(NSDictionary *)propertyDictionary{ //创建可变字典 NSMutableDictionary *dict = [NSMutableDictionary dictionary]; unsigned int outCount; objc_property_t *props = class_copyPropertyList([self class], &outCount); for(int i=0;i<outCount;i++){ objc_property_t prop = props[i]; NSString *propName = [[NSString alloc]initWithCString:property_getName(prop) encoding:NSUTF8StringEncoding]; id propValue = [self valueForKey:propName]; if(propValue){ [dict setObject:propValue forKey:propName]; } } free(props); return dict;}+ (NSArray *)classPropertyList { NSMutableArray *allProperties = [[NSMutableArray alloc] init]; unsigned int outCount; objc_property_t *props = class_copyPropertyList(self, &outCount); for (int i = 0; i < outCount; i++) { objc_property_t prop = props[i]; NSString *propName = [[NSString alloc]initWithCString:property_getName(prop) encoding:NSUTF8StringEncoding]; if (propName) { [allProperties addObject:propName]; } } free(props); return [NSArray arrayWithArray:allProperties];}@end
原文地址:https://github.com/shaojiankui/iOS-Categories/tree/master/Categories/Foundation/NSObject
0 0
- iOS 类增加成员变量
- 获取类的成员变量(ios)
- ios 成员变量 属性
- iOS 类成员变量和局部变量的问题
- iOS 类成员变量和局部变量的问题
- 分类中究竟能不能增加成员变量?
- Runtime 为分类增加成员变量
- IOS的成员变量、局部变量、全局变量
- ios 局部变量,全局变量,成员变量
- ios 属性(属性变量)和成员变量
- iOS 局部变量 全局变量 成员变量
- 项目记录-因为类增加了一个成员变量而导致的access violation
- iOS运行时判断某个类有那些成员变量
- IOS中"声明属性"和类成员变量的关系
- iOS --- Objective-C中类的成员变量与属性
- iOS访问类的私有成员变量及私有方法
- iOS访问类的私有成员变量及私有方法
- IOS开发中 类、对象、实例变量、成员变量、属性变量等区别和关系
- 数组实现栈的代码
- 【SpringMVC】下载功能
- Haskell之Yesod开发–边踩坑边开发(4)
- 引用第三方jar时,混淆代码时的异常(Proguard)
- git命令之git stash apply和 pop 的区别
- iOS 类增加成员变量
- SQL Script for select data from ebs and make a csv file to FTP
- C++ 怎么获得当前操作系统的语言,是英文版本还是简体或繁体
- UVa - 102 - Ecological Bin Packing
- 【SuperMap .Net 组件】应用3DMax制作3D模型
- 在Debug模式下中断, 在Release模式下跳出当前函数的断言
- html 、jsp笔记
- JS获取用户屏幕信息
- Android studio 閿欒 : 缂栫爜UTF-8鐨勪笉鍙 槧灏勫瓧绗?