I'm looking for a way to access, at runtime, only the properties of an object that were declared in the header file for that class. I was able to retrieve all the properties of an object via the following code:
MyTest *myTestObj = [[MyTest alloc] init];
myTestObj.prop1 = @"prop1";
myTestObj.prop2 = @"prop2";
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
unsigned count;
objc_property_t *properties = class_copyPropertyList([myTestObj class], &count);
for (int i = 0; i < count; i++) {
NSString *key = [NSString stringWithUTF8String:property_getName(properties[i])];
id keyValue = [myTestObj valueForKey:key];
if (keyValue != nil) {
[dict setObject:keyValue forKey:key];
}
}
free(properties);
(see Get an object properties list in Objective-C for more examples)
However, I need a way to limit the properties retrieved to only the ones declared in the .h file.
Basically, I'm looking for a way to access the object's public interface. Any suggestions on how to do this would be greatly appreciated.
I'm trying to save the state of my object in a dictionary so that it can be recreated later. Specifically, I'm trying to save it to the userInfo
property of UILocalNotification
.
I save the state of the object (in my case a UIViewController
) to the userInfo
property and create a local notification. When the user opens the app via a notification, I want to recreate the same UIViewController
and set its state to what it was at the time the notification was created.
If there is a better way to do this that doesn't involve hardcoding then I'd love to hear some suggestions.