iOS 类增加成员变量

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-11 03:28:30

iOS 类增加成员变量的相关文章

获取类的成员变量(ios)

获取类的成员变量(ios) unsigned int numIvars; Ivar *vars = class_copyIvarList(NSClassFromString(@"TestView"), &numIvars); NSString *key=nil; for(int i = 0; i < numIvars; i++) { Ivar thisIvar = vars[i]; key = [NSString stringWithUTF8String:ivar_get

【IOS 开发】Object - C 面向对象 - 类 , 对象 , 成员变量 , 成员方法

. 一. 类定义 类定义需要实现两部分 : -- 接口部分 : 定义类的成员变量和方法, 方法是抽象的, 在头文件中定义; -- 实现部分 : 引入接口部分的头文件, 实现抽象方法; 1. 接口部分定义 (1) 接口部分定义格式 接口部分定义格式 : @interface className : superClassName { type _variableName; ... ... } - (type) methodName : type parameterName; @end -- 接口定义

fielderror里的fieldName代表的是jsp里的fieldName还是Action类的成员变量?(待解答)

1.值栈的Action对象中会有一个fielderror属性,代表着字段错误. fielderror是Map<String,List<String>>类型 例如下面的值栈里可看到,fielderror属性里有Map, 键:ppt 值:[^The file is too large to be uploaded:ppt "FuzzyOpinionFigure1.fig" "upload_4fd387d7_8e03_479e_bf04_08e69368e3

类的成员变量和属性描述

0x 01 .类的属性 property 类的属性即是通过@property声明的属性.属性是类型为objc_property的一个结构体.该结构体封装了属性的信息 比如属性的名字,属性的类型,属性的可读写,非原子/原子属性等. 1).获取一个类的属性列表方法:OBJC_EXPORT objc_property_t *class_copyPropertyList(Class cls, unsigned int *outCount)入参是一个类的class对象,一个是用于统计属性数量的整形数的地址

在类有成员变量的场景下, 按照虚表原理, 模拟虚函数实现

前言 当类没有成员变量的情况下,   类首地址有4个字节的空间, 这里可以放我们模拟出来的虚表入口地址. 当类有成员变量的情况下, 类首地址就是成员变量,  所以, 为了模拟虚表实现, 需要在成员变量前, 再定义一个int型变量, 用来存放模拟的虚表入口地址. 现在还得不到虚析构函数的地址, 暂时按照非虚析构函数进行模拟. 这个实验是在C++中模拟的. 模拟虚函数实现的用途 在非OOP语言(C语言)中, 模拟类的实现, 可以实现虚函数的效果. 效果 工程下载点 编译环境: vc6sp6 + wi

解决&quot;VC6.0的ClassView里不能显示类或成员变量&quot;问题

VC6.0是微软1998年发布的,是一款很经典的编辑器,然而它有几个很常见的bug,比如, .cpp文件打不开,智能提示出现异常,这里介绍"VC6.0的ClassView里不能显示类或成员变量"问题的解决方法.详细步骤如下: 1) 关闭VC6.0,找到工程目录里的.clw文件,按Del键删除该 .clw文件,如图(1)所示: 图(1)按Del键删除.clw文件 2)打开VC6.0里的工程,按Ctrl+W –> OK,如图(2).图(3).图(4)所示: 图(2)点击"是

JavaSE8基础 final修饰类的成员变量,其只可以被访问,不能被修改

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        代码: /* final 修饰类的成员变量. 这个变量可以被访问,但是不能被修改.因为这个变量相当于常量. */ class Son { public final int num = 1; } class Demo { public static void main(String[] agrs) { Son s =

类作为成员变量

声明定义一个类的时候,成员变量的类型经常有int,String等,其实看源码知道String也是一个类: 说明是可以用类作为成员变量的: 其中,构造方法用于创建对象时候调用,new的时候jvm默认调用,可以直接new无参的(人),也可以直接new有参赋值的(小明——身高体重等): 类作为成员变量时候,在成员方法中必须要用这个类变量点它的属性来用,不能直接用,直接用是在栈内存中的一个地址值: set/get方法用于调用赋值或者获取值: 原文地址:https://www.cnblogs.com/wm

iOS运行时,如何增加成员变量

必须实现getter和setter方法 ``` - (void)setName:(NSString *)name { /** * 为某个类关联某个对象 * * @param object#> 要关联的对象 description#> * @param key#> 要关联的属性key description#> * @param value#> 你要关联的属性 description#> * @param policy#> 添加的成员变量的修饰符 descripti