https://blog.csdn.net/crazychickone/article/details/36413671/
/* 注意:要先導(dǎo)入ObjectC運(yùn)行時(shí)頭文件灸促,以便調(diào)用runtime中的方法*/
#import?
@implementationNSObject (PropertyListing)
1、/* 獲取對象的所有屬性膏秫,不包括屬性值 */
- (NSArray?*)getAllProperties
{
? ?u_int?count;???
objc_property_t?*properties? =class_copyPropertyList([self?class], &count);?
NSMutableArray?*propertiesArray = [NSMutableArray?arrayWithCapacity:count];
for(int?i =0; i
? ? {
const?char* propertyName =property_getName(properties[i]);
[propertiesArray?addObject: [NSString?stringWithUTF8String: propertyName]];
? ? }? ?
free(properties);
return?propertiesArray;?
}??
2侮措、/* 獲取對象的所有屬性 以及屬性值 */
- (NSDictionary*)properties_aps
{
? ?NSMutableDictionary?*props = [NSMutableDictionarydictionary];? ?
unsignedintoutCount, i;
objc_property_t*properties =class_copyPropertyList([selfclass], &outCount);
for(i =0; i
? ? {
objc_property_tproperty = properties[i];
constchar* char_f =property_getName(property);
NSString*propertyName = [NSString?stringWithUTF8String:char_f];
idpropertyValue = [self?valueForKey:(NSString*)propertyName];
if(propertyValue) [propssetObject:propertyValueforKey:propertyName];
? ? }? ?
free(properties);
returnprops;
}? ?
3懈叹、/* 獲取對象的所有方法 */
-(void)printMothList
{
unsignedintmothCout_f =0;
Method* mothList_f =class_copyMethodList([selfclass],&mothCout_f);
for(inti=0;i
? ? {
Methodtemp_f = mothList_f[i];
IMPimp_f =method_getImplementation(temp_f);
SELname_f =method_getName(temp_f);
constchar* name_s =sel_getName(method_getName(temp_f));
intarguments =method_getNumberOfArguments(temp_f);
constchar* encoding =method_getTypeEncoding(temp_f);
NSLog(@"方法名:%@,參數(shù)個(gè)數(shù):%d,編碼方式:%@",[NSStringstringWithUTF8String:name_s],
arguments,[NSString?stringWithUTF8String:encoding]);
? ? }
free(mothList_f);
}
@end?