Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check a class in a NSDictionary

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]

like image 914
Nakkeeran Avatar asked Dec 17 '22 05:12

Nakkeeran


2 Answers

Look up -isKindOfClass: and -isMemberOfClass:

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

See the Apple isKindOfClass: documentation.

like image 139
Richard Stelling Avatar answered Jan 01 '23 00:01

Richard Stelling


- (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.

like image 36
Totumus Maximus Avatar answered Dec 31 '22 23:12

Totumus Maximus