1

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
zacharyscott
  • 73
  • 1
  • 5
  • Don't do it, clarity wins over clever. – zaph Jan 14 '14 at 19:41
  • 1
    That code doesn't actually do anything. If you wanted to explain what you're actually trying to do, we might be able to suggest a realistic way to do it. – Chuck Jan 14 '14 at 19:47

1 Answers1

1

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.

Chuck
  • 234,037
  • 30
  • 302
  • 389