5

Suppose you have a class "Foo" with property "testProperty". The aim is to get property name (not value) as NSString. The question is probably duplicate of Get property name as a string . But that answers did not help me because:

  • I don't need all properties. I need only particular one.
  • For NSStringFromSelector(@selector(prop)) - anyway you have to input string.

Suppose I have a function like

- (NSString *)propertyToString:(id)propertyOfObject

In that case I can use, for instance, for sorting

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[self propertyToString:foo.testProperty] ascending:NO]; 

So finally, the code will be clean.

Community
  • 1
  • 1
razor28
  • 1,077
  • 10
  • 17
  • 5
    You said - "I want one particular, not all."...uhm..how exactly would you know which particular one you want, if you didn't know its name? And IF you knew, well just create the string yourself...? – Michal Feb 05 '14 at 10:00
  • 1
    Your question is a duplicate to the one you linked. Just do as in the answer you linked, and when looping over all properties, just store the one you want and ignore the rest. – Volker Feb 05 '14 at 10:02
  • @Michal,I would like to make my code clean, that is why I don't want to use string constants. – razor28 Feb 05 '14 at 10:03
  • @razor28 It's not clean to hide the name of the property you're using. Imagine the person that takes over your code after you switch to different project. Will he/she know immediately what is going on in your code, if you actually incorporate the name of the property into string, that is not able to be seen unless it's actually running in lldb? – Michal Feb 05 '14 at 10:09
  • @Volker, in that case how do I convert Foo.testProperty to objc_property_t ? – razor28 Feb 05 '14 at 10:10
  • @Michal, suppose I have a function like - (NSString *)propertyToString:(id)propertyOfObject. In that case I can use, for instance, for sorting NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[self propertyToString:foo.testProperty] ascending:NO]; I guess everything is clean. – razor28 Feb 05 '14 at 10:15
  • Then do something like #define mySuperSortKey @"testProperty" and use mySuperSortKey – Volker Feb 05 '14 at 10:25
  • Is the idea to get the property name based on what object it is currently set to? – JeremyP Feb 05 '14 at 11:52
  • @JeremyP, instance variable of object doesn't matter. The idea is not to use string constants or config files for mapping or comparing purposes. – razor28 Feb 05 '14 at 12:07
  • 1
    It's now clear what this question is asking, the question has been updated with a practical example. Please reopen. – WDUK Feb 05 '14 at 13:17
  • You can't do what you're asking for. `foo.testProperty` is simply a method call (really, a message send), not some kind of "property entity" like it might be in another language. Using the name of the property is how you do this in ObjC. `@selector(nameOfMethod)` is how you refer to methods, including properties' setters and getters, as things in their own right. – jscs Feb 05 '14 at 19:52
  • It's possible, but not at run time, pre-processor macros can achieve this job. – WDUK Feb 07 '14 at 13:18
  • From [Get property name as string, without using the runtime reference library](http://www.g8production.com/post/78429904103/get-property-name-as-string-without-using-the), just define: `#define propertyKeyPath(property) (@""#property)` `#define propertyKeyPathLastComponent(property) [[(@""#property) componentsSeparatedByString:@"."] lastObject]` And then you can do something like this: `NSLog(@"%@", propertyKeyPathLastComponent(appleStore.storeLocation.street)); //result: street` – samthui7 Jun 17 '15 at 10:03

2 Answers2

0

It is impossible. You want to get the property name but you need to pass something to distinguish it from other ones.

Dinh Quan
  • 1,257
  • 2
  • 11
  • 10
0

Code's not tested. Should be sth like:

#import <objc/runtime.h>

Class cls = [Foo class];
u_int count;
objc_property_t* props = class_copyPropertyList(cls,&count);
for(int i=0;i<count;i++){
    NSString* propname = [NSString stringWithUTF8String:property_getName(props[i])];
    if([propname hasPrefix:@"test"])
        NSLog(@"foo");
}

Your searching a way to select a subset of the prop sets. Only thing you can do is apply some kind of filter - could be a naming convention of properties like above - to reduce the set of properties to your desired ones.

Schemiii
  • 346
  • 4
  • 15