iphone - Objective-C: How to check if 2 properties are the same or not? -
let's have 2 instances of person
class. 1 named john , 1 mary. person
class has 2 properties age
, gender
. there way make iteration thorough instances' properties , check if current property equal given property? this:
for (iterate thorough properties of instance mary) { //first iteration @selector(mary.age)==@selector(john.age) //this yes; //second iteration @selector(mary.gender)==@selector(john.age) //this no; }
you can property name nsstrings
, use isequaltostring
: method compare them.
for (iterate thorough 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"); }
Comments
Post a Comment