How to find all property keys of an object compatible with KVC Objective-C?

Is there a method that returns all the keys for an object that conforms to the NSKeyValueCoding protocol?

Something along the lines of [object getPropertyKeys] that will return an NSArray of NSString objects. It will work for any KVC compatible facility. Is there such a method? So far, I have not found anything in the search for Apple documents.

Thanks G.

+23
properties objective-c cocoa introspection key-value-observing
Apr 23 '09 at 9:10
source share
5 answers
 #import "objc/runtime.h" unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([self class], &outCount); for(i = 0; i < outCount; i++) { objc_property_t property = properties[i]; const char *propName = property_getName(property); if(propName) { const char *propType = getPropertyType(property); NSString *propertyName = [NSString stringWithUTF8String:propName]; NSString *propertyType = [NSString stringWithUTF8String:propType]; } } free(properties); 
+32
Apr 23 '09 at 9:22
source share

Use class_getPropertyList . This will tell you all the @properties object.

It will not necessarily list each property compatible with KVC, because any method that takes no arguments and returns a value is a valid GVC-compatible receiver. There is no 100% reliable way for the runtime to know which of them behave as properties (for example, -[NSString length] ) and which of them behave as commands (for example, -[NSFileHandle readDataToEndOfFile] ).

In any case, you should declare your KVC compatible properties as @properties , so this should not be too big a problem.

+3
Apr 23 '09 at 9:23
source share

There is no method like the KVO system does not require objects / classes to register which properties they support KVO. Any key can potentially support KVO, the only way to find out from the documentation of the author.

And, of course, there is no guarantee that @property will support KVO; It is possible to write a property that is not (and may be necessary sometimes). So, getting a list of the @property class, and then assuming they are KVO compatible, would be a dangerous choice, in my opinion.

+1
Apr 24 '09 at 12:40
source share

You need the getPropertyType function. See This Post: Get a List of Object Attributes in Objective-C

0
Oct 24 2018-10-10
source share

Environment: Xcode7.2, iOS9.2

I use the code snippet below to get the property names of the specified class, it works fine:

import "objc / runtime.h"

 unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([SNBankBean class], &outCount); for(i = 0; i < outCount; i++) { objc_property_t property = properties[i]; const char *propName = property_getName(property); if(propName) { NSString *propertyName = [NSString stringWithUTF8String:propName]; NSLog(@"the property name is %@",propertyName); if ([propertyName isEqualToString:bank_name]) { self.isBankExisted=YES; } } } free(properties); 
0
Mar 18 '16 at 8:15
source share



All Articles