1

Let's say I have 2 instances of Person class. One named john and one mary. The Person class has 2 properties age and gender. Is there a way to make an iteration thorough all the instances' properties and check if the current property is equal to a given property? Something like this:

for (iterate thorough all properties of instance mary) {
//first iteration
@selector(mary.age)==@selector(john.age) //this would be YES;

//second iteration
@selector(mary.gender)==@selector(john.age) //this would be NO;
}
Rad'Val
  • 8,895
  • 9
  • 62
  • 92

3 Answers3

3

This question addresses how to list the selectors that an object will respond to:

List selectors for Objective-C object

Using a combination of that and the NSObject protocol's -respondsToSelector: method, you could list all the selectors for john, check if mary responds to them, and vice versa.

Community
  • 1
  • 1
alexantd
  • 3,543
  • 3
  • 27
  • 41
  • Well, I probably didn't made myself clear in the question, but this is not what I'm looking for. This checks if 2 objects have the same values for the 2 properties they have: `age` and `gender`. I'm looking for a way to compare the properties themselves, not the values they hold (aka `age` is not the same as `gender` but `age` is the same with `age` no matter the value). Know what I mean? – Rad'Val Jul 21 '11 at 21:42
  • I see that you updated your answer. + 1 for mentioning the linked question, that helps a lot too. However, I'm not looking to find if the second object responds to one of the first object's selector (it will always do, because they are of the same class). I'm looking check if the current iterated property is the same with a given property (not as value, as "name". eg: propery `age` form mary equals propery `age` from john, even if john is 20yrs old and mary 30) – Rad'Val Jul 21 '11 at 21:53
  • I'm afraid I still don't get it. :( You can compare selector names or method return values, but I don't know what else you could compare. Note that properties in ObjC 2.0 are just auto-generated accessor methods, which respond to selectors like any other method. – alexantd Jul 21 '11 at 21:59
  • well, as you said:) : `you can compare selector names` that's what I want to do. However, `-respondsToSelector:` doesn't compare the selector names, only checks if a certain object responds to a certain message. – Rad'Val Jul 21 '11 at 22:03
  • Right - so the steps would be: 1) Use sel_getName() to list all of john's selectors; 2) for each selector, check if mary responds to it using -respondsToSelector:. 3) Repeat 1 & 2 with mary & john reversed. 4) If the result of every call to -respondsToSelector: is YES, then their selector names are equal. If not, then either john has one that mary doesn't have, or vice versa. – alexantd Jul 21 '11 at 22:15
1

See Printing all declared properties of an object to iterate and get the name of properties. You may need to add them to a set and then compare. If you want to check the type it may help you to read Declared Properties in the objc runtime guide.

Jano
  • 62,815
  • 21
  • 164
  • 192
  • The second linked article did hit the spot. Thanks! I found `const char *property_getName(objc_property_t property)` which I can use in correlation with the iteration to solve my problem. Wanted to choose your answer but Cyprian provided code to the community:)...and to myself:P. +1 anyway – Rad'Val Jul 21 '11 at 22:08
1

You can get the property name as NSStrings and use isEqualToString: method to compare them.

for (iterate thorough all properties of instance mary) {

//first iteration
NSString *marryProperty = [NSString stringWithCString:property_getName(mary.age)
                                            encoding:NSUTF8StringEncoding];
NSString *johnProperty = [NSString stringWithCString:property_getName(john.age)
                                            encoding:NSUTF8StringEncoding];

if([marryProperty isEqualToString:johnProperty])
   NSLog(@"YES");
else 
   NSLog(@"NO");
}
Cyprian
  • 9,423
  • 4
  • 39
  • 73