0

I am reading a text file. I want to check whether there is a variable declared in the class with name same as the text that I read from the file.

Method for this in java is 'java.lang.Class.getDeclaredField()'. See http://www.tutorialspoint.com/java/lang/class_getdeclaredfield.htm for details.

I was unable to find similar method in ObjectiveC. Is there any? If no, how I can implement the same. Please give me few tips for that if you get any idea.

2 Answers2

1

You can check like this: /for properties/

YourClass *arrObj=[YourClass new];//your target class where you wnat to check
NSString *propertyName=@"samllArray";//this is what you will check in class YourClass
if([arrObj respondsToSelector:NSSelectorFromString(propertyName)]){
    NSLog(@"yes, exists");
}
else{
    NSLog(@"no, it does not exists");
}

EDIT:/for ivars/

- (NSMutableArray *)getAllPropertyOfClass:(Class)aClass {
    NSMutableArray *mArray=[NSMutableArray new];
    unsigned int outCount;
    Ivar *ivars = class_copyIvarList([aClass class], &outCount); //class_copyPropertyList([aClass class], &outCount);

    for(unsigned int i = 0; i < outCount; i++) {
        Ivar ivar = ivars[i];
        const char *propName = ivar_getName(ivar);
        if(propName) {
            NSString *propertyName = [NSString stringWithUTF8String:propName];
            mArray[mArray.count]=propertyName;
        }
    }
    free(ivars);
    return mArray;
}
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
-1

There are respondsToSelector and conformsToProtocol methods maybe they could help

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • 1
    Those have nothing to do with dynamic introspection/reflection. A field in Java is analogous to an iVar, not a protocol. – CodaFi Apr 09 '13 at 06:17