通過runtime列印出物件所有屬性的值

2021-09-23 21:52:04 字數 2893 閱讀 6184

通過runtime列印出物件所有屬性的值

今天給給大家提供的關於nsobject的category,通過runtime列印屬性的值,相當有用哦,以後你再也不用每個物件都通過nslog來逐個列印屬性值了。

原始碼:nsobject+properties.h 與 nsobject+properties.m

//

// nsobject+properties.h

//// created by youxianming on 14-9-4.

//#import @inte***ce nsobject (properties)

- (nsdictionary*)propertiesvalues;

@end

//

// nsobject+properties.m

//// created by youxianming on 14-9-4.

//#import "nsobject+properties.h"

#import @implementation nsobject (properties)

- (nsdictionary*)propertiesvalues

#pragma private - 私有方法

// 獲取乙個類的屬性名字列表

- (nsarray*)propertynames:(class)class

free(properties);

return propertynames;

}// 根據屬性陣列獲取該屬性的值

- (nsdictionary*)propertiesandvaluesdictionary:(id)obj properties:(nsarray *)properties

propertiesvaluesdic[property] = valueobj;}}

return propertiesvaluesdic;

}@end

//

// nsobject+properties.m

//// created by youxianming on 14-9-4.

//#import "nsobject+properties.h"

#import @implementation nsobject (properties)

- (nsdictionary*)propertiesvalues

#pragma private - 私有方法

// 獲取乙個類的屬性名字列表

- (nsarray*)propertynames:(class)class

free(properties);

return propertynames;

}// 根據屬性陣列獲取該屬性的值

- (nsdictionary*)propertiesandvaluesdictionary:(id)obj properties:(nsarray *)properties

propertiesvaluesdic[property] = valueobj;}}

return propertiesvaluesdic;

}@end

使用詳情:

測試用model

//

// yxmodel.h

// test

//// created by youxianming on 14-9-4.

//#import @inte***ce yxmodel : nsobject

@property (nonatomic, strong) nsstring *name;

@property (nonatomic, strong) nsnumber *age;

@property (nonatomic, strong) nsstring ****;

@property (nonatomic, strong) nsdictionary *info;

@end

//

// yxmodel.m

// test

//// created by youxianming on 14-9-4.

//#import "yxmodel.h"

@implementation yxmodel

@end

使用:

//

// test

//// created by youxianming on 14-9-4.

//#import "nsobject+properties.h"

#import "yxmodel.h"

;

// 列印出所有屬性的值

nslog(@"%@", model.propertiesvalues);

return yes;

}@end

列印資訊:

通過runtime列印出物件所有屬性的值

通過runtime列印出物件所有屬性的值 今天給給大家提供的關於nsobject的category,通過runtime列印屬性的值,相當有用哦,以後你再也不用每個物件都通過nslog來逐個列印屬性值了。原始碼 nsobject properties.h 與 nsobject properties.m...

如何列印出所有的物件?

其實是能實現的,通過get class 函式 複製 mdb new mysql com new common all vars array keys get defined vars sys vars array globals env http env vars post http post va...

python 列印物件的所有屬性值的方法

如下所示 def prn obj obj print n join s s item for item in obj.dicoisiteyt items 以上這篇python 列印物件的所有屬性值的方法就是小編分享給大家的www.cppcns.com全部內容了www.cppcns.com,希望能給大...