Подтвердить что ты не робот

Список свойств класса в Objective-C

Есть ли способ получить массив свойств класса определенного типа? Например, если у меня есть интерфейс, подобный этому

@interface MyClass : NSObject
    @property (strong,nonatomic) UILabel *firstLabel;
    @property (strong,nonatomic) UILabel *secondLabel;        
@end

могу ли я получить ссылку на эти метки в реализации, не зная их имени?

@implementation MyClass
    -(NSArray*)getListOfAllLabels
    {
            ?????
    }        
@end

Я знаю, что могу сделать это легко с помощью [NSArray arrayWithObjects:firstLabel,secondLabel,nil], но я хотел бы сделать это с помощью какой-то нумерации классов, например for (UILabel* oneLabel in ???[self objects]???)

4b9b3361

Ответ 1

Итак, точнее, вам нужна динамическая проверка работоспособности свойств, если я правильно ее понял. Сделайте что-нибудь подобное (реализуйте этот метод самостоятельно, класс, который вы хотите интроспектировать):

#import <objc/runtime.h>

- (NSArray *)allPropertyNames
{
    unsigned count;
    objc_property_t *properties = class_copyPropertyList([self class], &count);

    NSMutableArray *rv = [NSMutableArray array];

    unsigned i;
    for (i = 0; i < count; i++)
    {
        objc_property_t property = properties[i];
        NSString *name = [NSString stringWithUTF8String:property_getName(property)];
        [rv addObject:name];
    }

    free(properties);

    return rv;
}

- (void *)pointerOfIvarForPropertyNamed:(NSString *)name
{
    objc_property_t property = class_getProperty([self class], [name UTF8String]);

    const char *attr = property_getAttributes(property);
    const char *ivarName = strchr(attr, 'V') + 1;

    Ivar ivar = object_getInstanceVariable(self, ivarName, NULL);

    return (char *)self + ivar_getOffset(ivar);
}

Используйте его следующим образом:

SomeType myProperty;
NSArray *properties = [self allPropertyNames];
NSString *firstPropertyName = [properties objectAtIndex:0];
void *propertyIvarAddress = [self getPointerOfIvarForPropertyNamed:firstPropertyName];
myProperty = *(SomeType *)propertyIvarAddress;

// Simpler alternative using KVC:
myProperty = [self valueForKey:firstPropertyName];

Надеюсь, что это поможет.

Ответ 2

использовать метод attributeKeys для NSObject.

    for (NSString *key in [self attributeKeys]) {

        id attribute = [self valueForKey:key];

        if([attribute isKindOfClass:[UILabel  class]])
        {
         //put attribute to your array
        }
    }

Ответ 3

Откроется ссылка . Это объектная c-оболочка для объектной среды выполнения C.

Вы можете использовать код, как показано ниже

uint count;
objc_property_t* properties = class_copyPropertyList(self.class, &count);
    NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i < count ; i++)
    {
        const char* propertyName = property_getName(properties[i]);
        [propertyArray addObject:[NSString  stringWithCString:propertyName encoding:NSUTF8StringEncoding]];
    }
    free(properties);

Ответ 4

Вы должны включать заголовки времени выполнения

 #import<objc/runtime.h>
uint propertiesCount;
objc_property_t *classPropertiesArray = class_copyPropertyList([self class], &propertiesCount);
free(classPropertiesArray);

Ответ 5

Ответ @user529758 не будет работать с ARC, и он не будет перечислять свойства каких-либо классов-предков.

Чтобы исправить это, вам нужно пройти иерархию классов и использовать ARC-совместимый [NSObject valueForKey:] для получения значений свойств.

Person.h:

#import <Foundation/Foundation.h>

extern NSMutableArray *propertyNamesOfClass(Class klass);

@interface Person : NSObject

@property (nonatomic) NSString *name;

@end

Person.m:

#import "Person.h"
#import <objc/runtime.h>

NSMutableArray *propertyNamesOfClass(Class klass) {
    unsigned int count;
    objc_property_t *properties = class_copyPropertyList(klass, &count);

    NSMutableArray *rv = [NSMutableArray array];

    for (unsigned int i = 0; i < count; i++)
    {
        objc_property_t property = properties[i];
        NSString *name = [NSString stringWithUTF8String:property_getName(property)];
        [rv addObject:name];
    }

    free(properties);

    return rv;
}

@implementation Person

- (NSMutableArray *)allPropertyNames {
    NSMutableArray *classes = [NSMutableArray array];
    Class currentClass = [self class];
    while (currentClass != nil && currentClass != [NSObject class]) {
        [classes addObject:currentClass];
        currentClass = class_getSuperclass(currentClass);
    }

    NSMutableArray *names = [NSMutableArray array];
    [classes enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(Class currentClass, NSUInteger idx, BOOL *stop) {
        [names addObjectsFromArray:propertyNamesOfClass(currentClass)];
    }];

    return names;
}

- (NSString*)description {
    NSMutableArray *keys = [self allPropertyNames];
    NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithCapacity:keys.count];
    [keys enumerateObjectsUsingBlock:^(NSString *key, NSUInteger idx, BOOL *stop) {
        properties[key] = [self valueForKey:key];
    }];

    NSString *className = NSStringFromClass([self class]);
    return [NSString stringWithFormat:@"%@ : %@", className, properties];
}

Student.h:

#import "Person.h"

@interface Student : Person

@property (nonatomic) NSString *studentID;

@end

Student.m:

#import "Student.h"

@implementation Student

@end

main.m:

#import <Foundation/Foundation.h>
#import "Student.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        Student *student = [[Student alloc] init];
        student.name = @"John Doe";
        student.studentID = @"123456789";
        NSLog(@"student - %@", student);
    }
    return 0;
}

Ответ 6

Решение serhats велико, к сожалению, оно не работает для iOS (как вы упомянули) (и этот вопрос отмечен для iOS). Обходным решением было бы получить представление NSDictionary объекта, а затем получить доступ к нему как к парам ключ-значение. Я бы рекомендовал категорию для NSObject:

Header файл:

@interface NSObject (NSDictionaryRepresentation)

/**
 Returns an NSDictionary containing the properties of an object that are not nil.
 */
- (NSDictionary *)dictionaryRepresentation;

@end

Реализация файл:

#import "NSObject+NSDictionaryRepresentation.h"
#import <objc/runtime.h>

@implementation NSObject (NSDictionaryRepresentation)

- (NSDictionary *)dictionaryRepresentation {
    unsigned int count = 0;
    // Get a list of all properties in the class.
    objc_property_t *properties = class_copyPropertyList([self class], &count);

    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithCapacity:count];

    for (int i = 0; i < count; i++) {
        NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
        NSString *value = [self valueForKey:key];

        // Only add to the NSDictionary if it not nil.
        if (value)
            [dictionary setObject:value forKey:key];
    }

    free(properties);

    return dictionary;
}

@end

Заимствован из этой статьи: http://hesh.am/2013/01/transform-properties-of-an-nsobject-into-an-nsdictionary/

Таким образом, вы могли бы сделать что-то подобное, как упомянутые серхаты:

for (NSString *key in objectDic.allKeys) {
   if([objectDic[key] isKindOfClass:[UILabel  class]])
   {
       //put attribute to your array
   }
}