27

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

Something along the lines of [object getPropertyKeys] that would return an NSArray of NSString objects. It would work for any KVC-compliant object. Does such a method exist? I haven't found anything in searching the Apple docs so far.

Thanks, G.

Quinn Taylor
  • 44,553
  • 16
  • 113
  • 131
armahg
  • 739
  • 2
  • 10
  • 11

5 Answers5

40
#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);
Julian F. Weinert
  • 7,474
  • 7
  • 59
  • 107
oxigen
  • 6,263
  • 3
  • 28
  • 37
  • Why has somebody modded this down? I'm interested to know what they think is wrong with it. – Mike Abdullah Apr 23 '09 at 12:22
  • 1
    Yeah. I've withdrawn my downmod. BTW, stringWithCString: is deprecated—use stringWithUTF8String: or stringWithCString:encoding: instead. – Peter Hosey Apr 23 '09 at 14:28
  • Very useful ... thanks!! I already knew that stringWithCString was deprecated – armahg Apr 24 '09 at 04:11
  • 5
    This code gathers only the properties declared directly in the object's class. To get all of the object's properties, you also need to walk the superclass chain and gather those class's properties. – Dale Emery Nov 19 '11 at 01:24
  • 11
    Where is this method: `getPropertyType(property)` defined? – chakrit Jun 18 '12 at 07:56
  • 2
    This only gets keys declared with the `@property` syntax. A class may be KVC-compliant for other keyPaths by virtue of implementing complying methods (or method pairs) and/or overriding the core KVC handling methods (i.e. valueForUndefinedKey:, setValue:forUndefinedKey:). There is no way to *definitively* get the keys that an arbitrary class is KVC-compliant for, unfortunately. Put differently, don't rely on this. – ipmcc Feb 22 '13 at 12:46
  • 2
    @chakrit he's likely defined a function that parses the result of property_getAttributes to rip out the type name. You can check out [this page](https://developer.apple.com/library/mac/documentation/cocoa/conceptual/objcruntimeguide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW6) for more details on this answer and related code. – Sandy Chapman Apr 04 '14 at 19:03
3

Use class_getPropertyList. That will tell you all the @properties of the object.

It won't necessarily list every KVC-compliant property, because any method that takes no arguments and returns a value is a valid KVC-compliant getter. There's no 100%-reliable way for the runtime to know which ones behave as properties (e.g., -[NSString length]) and which ones behave as commands (e.g., -[NSFileHandle readDataToEndOfFile]).

You should be declaring your KVC-compliant properties as @properties anyway, so this shouldn't be too big of a problem.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
1

There is no such method as the KVO system does not require objects/classes to register with it which properties they support KVO for. Any key could potentially support KVO, the only way to know is from the author's documentation.

And of course, there is no guarantee that an @property will support KVO; it's quite possible to write a property that doesn't (and may be necessary sometimes). So, getting a list of a class's @propertys and then assuming they're KVO-compliant would be a dangerous choice in my opinion.

Mike Abdullah
  • 14,933
  • 2
  • 50
  • 75
0

You need a getPropertyType function. See this post: Get an object attributes list in Objective-C

Community
  • 1
  • 1
Chatchavan
  • 91
  • 1
  • 2
0

For Swift onlookers, you can get this functionality by utilising the Encodable functionality. I will explain how:

  1. Conform your object to Encodable protocol

    class ExampleObj: NSObject, Encodable {
        var prop1: String = ""
        var prop2: String = ""
    }
    
  2. Create extension for Encodable to provide toDictionary functionality

     public func toDictionary() -> [String: AnyObject]? {
        let encoder = JSONEncoder()
        encoder.outputFormatting = .prettyPrinted
        guard let data =  try? encoder.encode(self),
              let json = try? JSONSerialization.jsonObject(with: data, options: .init(rawValue: 0)), let jsonDict = json as? [String: AnyObject] else {
            return nil
        }
        return jsonDict
    }
    
  3. Call toDictionary on your object instance and access keys property.

    let exampleObj = ExampleObj()
    exampleObj.toDictionary()?.keys
    
  4. Voila! Access your properties like so:

    for k in exampleObj!.keys {
        print(k)
    }
    // Prints "prop1"
    // Prints "prop2"
    
Harry Bloom
  • 2,359
  • 25
  • 17