Loop through all object properties at runtime

前端 未结 3 1260
不思量自难忘°
不思量自难忘° 2020-12-05 05:59

I want to create an Objective-C base class that performs an operation on all properties (of varying types) at runtime. Since the names and types of the properties will not a

相关标签:
3条回答
  • 2020-12-05 06:26

    Adding some detial to @mvds:

    unsigned int count=0;
    objc_property_t *props = class_copyPropertyList([self class],&count);
    for ( int i=0;i<count;i++ )
    {
        const char *name = property_getName(props[i]);
        NSString* dataToGet = [NSString swf:@"%s",name];
        @try { // in case of this pair not key value coding-compliant
            id value = [barButton valueForKey:dataToGet];
            NSLog(@"prop %d: %s  %@",i,name, value);
        } @catch (NSException *exception) {
            // NSLog(@"Exception:%@",exception);
        }
        @finally {
            // Display Alternative
        }
    }
    

    Please give @mvds the upvote.

    0 讨论(0)
  • 2020-12-05 06:38

    Use

    objc_property_t * class_copyPropertyList(Class cls, unsigned int *outCount)
    

    and read https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html on how to do this exactly.

    Some code to get you going:

    #import <objc/runtime.h>
    
    unsigned int count=0;
    objc_property_t *props = class_copyPropertyList([self class],&count);
    for ( int i=0;i<count;i++ )
    {
        const char *name = property_getName(props[i]); 
        NSLog(@"property %d: %s",i,name);
    }
    
    0 讨论(0)
  • 2020-12-05 06:45

    To expand on mvds' answer (started writing this before I saw his), here's a little sample program that uses the Objective-C runtime API to loop through and print information about each property in a class:

    #import <Foundation/Foundation.h>
    #import <objc/runtime.h>
    
    @interface TestClass : NSObject
    
    @property (nonatomic, retain) NSString *firstName;
    @property (nonatomic, retain) NSString *lastName;
    @property (nonatomic) NSInteger *age;
    
    @end
    
    @implementation TestClass
    
    @synthesize firstName;
    @synthesize lastName;
    @synthesize age;
    
    @end
    
    int main(int argc, char *argv[]) {
        @autoreleasepool {
            unsigned int numberOfProperties = 0;
            objc_property_t *propertyArray = class_copyPropertyList([TestClass class], &numberOfProperties);
    
            for (NSUInteger i = 0; i < numberOfProperties; i++)
            {
                objc_property_t property = propertyArray[i];
                NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)];
                NSString *attributesString = [[NSString alloc] initWithUTF8String:property_getAttributes(property)];
                NSLog(@"Property %@ attributes: %@", name, attributesString);
            }
            free(propertyArray);
        }
    }
    

    Output:

    Property age attributes: T^q,Vage
    Property lastName attributes: T@"NSString",&,N,VlastName
    Property firstName attributes: T@"NSString",&,N,VfirstName

    Note that this program needs to be compiled with ARC turned on.

    0 讨论(0)
提交回复
热议问题