4

In the below dictionary I want to write a condition for type class, Is there any way to identify the class type alone in the iteraction

NSDictionary *dictionary = [NSDictionary dictionaryWithKeysAndObjects:@"check",@"checkValue",@"webservice", [webservice class], @"list",@"listValue", nil, @"task", [task class], @"new", @"newValue", @"operation",[operation class]];

  for(NSString *aKey in dictionary) {        

        if ([[dictionary valueForKey:aKey]) {
            NSLog(@"Getting In");
        }

    }

Note :I want a single condition to check values [webservice class],[task class],[operation class]

Nakkeeran
  • 15,296
  • 1
  • 21
  • 22

3 Answers3

9

Look up -isKindOfClass: and -isMemberOfClass:

if([object isKindOfClass:[AObjectClass class])
{
    NSLog(@"object is of type AObjectClass");
}

See the Apple isKindOfClass: documentation.

Richard Stelling
  • 25,607
  • 27
  • 108
  • 188
  • It is used to check a particular class, I just want generic solution to check whether its a class alone – Nakkeeran Jan 16 '12 at 11:17
2
- (BOOL)isMemberOfClass:(Class)aClass

this is what u seek probably.

For example, in this code, isMemberOfClass: would return NO:

NSMutableData *myData = [NSMutableData dataWithCapacity:30];
id anArchiver = [[NSArchiver alloc] initForWritingWithMutableData:myData];
if ([anArchiver isMemberOfClass:[NSCoder class]])
    //something...

ref: http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html


Edit:

In NSDictionaries you will have to put Strings for keys. I suggest you convert the class to a string with NSStringFromClass([MyClass class]); and put that as a key.

The way you want to use the class as a key is impossible.

Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69
  • It is used to check a particular class, I just want generic solution to check whether its a class alone – Nakkeeran Jan 16 '12 at 11:15
  • @Nakkeeran: the way you want to use the class as a key is impossible i suggest a work-around using the method in my answer as key. – Totumus Maximus Jan 16 '12 at 11:30
1

Found answer for my question in the below link

Check if object is Class type

NSDictionary *dictionary = [NSDictionary dictionaryWithKeysAndObjects:@"check",@"checkValue",@"webservice", [webservice class], @"list",@"listValue", nil, @"task", [task class], @"new", @"newValue", @"operation",[operation class]];

  for(NSString *aKey in dictionary) {        

        if (class_isMetaClass(object_getClass(obj))) {
            NSLog(@"Getting In");
        }

    }
Community
  • 1
  • 1
Nakkeeran
  • 15,296
  • 1
  • 21
  • 22