Identifying properties in the header file vs. implementation file

121 Views Asked by At

I have been interested in using something along the the lines of the following code to automate the building of my objects (since there are many of them with quite a few properties):

MyObject *myObject = [[myObject alloc] init];

unsigned int numberOfProperties = 0;
objc_property_t *propertyArray = class_copyPropertyList([MyObject class], &numberOfProperties);

for (NSUInteger i = 0; i < numberOfProperties; i++)
{
    objc_property_t property = propertyArray[i];

    NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];

    if (propertyName)
    {
        id valueForProperty = [myObject valueForKey:propertyName];
        [myObject setValue:valueForProperty forKey:propertyName];
    }

}
free(propertyArray);

However, what I've noticed is that this code will try to run not just on the properties in my header file, but also all of my implementation properties as well, which I do not want.

Since Objective-C doesn't actually distinguish public vs private properties, I am not sure how to do this. Any thoughts on how to indicate that I'm only interested in the properties in the header file to simulate the same thing effectively?

1

There are 1 best solutions below

0
On

In short, you don't. This information is not available in the compiled program. You'd need to write a custom preprocessor to do this if you really wanted to.