Objective-c runtime - get property list of NSObject protocol returning nil

950 views Asked by At

I've been trying to get the list of the properties from the NSObject protocol, but I've been having some trouble.

Using the following code to seems to constantly return nil fro the list of properties on that protocol.

Protocol *protocol = objc_getProtocol("NSObject");

NSLog(@"%s", protocol_getName(protocol)); // confirms that NSObject protocol is found

unsigned count;

objc_property_t *properties = protocol_copyPropertyList(protocol, &count);

for (NSUInteger index = 0; index < count; index++) {

    NSString *key = [NSString stringWithUTF8String:property_getName(properties[index])];

    [propertiesArray addObject:key];

}

free(properties);

NSLog(@"%@",propertiesArray); // prints empty array 

By all accounts this should work. I have even created my own protocol and used it in place of NSObject and it works fine.

@protocol TestProtocol

@property NSString *someSortOfProperty;

@end

The same code as above with only the name of the protocol changed returns the someSortOfProperty property.

Protocol *protocol = objc_getProtocol("TestProtocol");

NSLog(@"%s", protocol_getName(protocol)); // confirms that TestProtocol protocol is found

unsigned count;

objc_property_t *properties = protocol_copyPropertyList(protocol, &count);

for (NSUInteger index = 0; index < count; index++) {

    NSString *key = [NSString stringWithUTF8String:property_getName(properties[index])];

    [propertiesArray addObject:key];

}

free(properties);

NSLog(@"%@",propertiesArray); // prints [85711:303] ( someSortOfProperty )

If any of you have succeeded in doing this in the past, or can spot something that I've done wrong that would be great.

0

There are 0 answers