如何在NSDictionary中检查类

时间:2021-07-23 13:49:57

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

在下面的字典中,我想为type class编写一个条件,是否有办法在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]

注意:我想要一个条件来检查值[webservice类],[task类],[operation类]

3 个解决方案

#1


8  

Look up -isKindOfClass: and -isMemberOfClass:

查找-isKindOfClass:和-isMemberOfClass:

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

See the Apple isKindOfClass: documentation.

参见Apple isKindOfClass:文档。

#2


2  

- (BOOL)isMemberOfClass:(Class)aClass

this is what u seek probably.

这可能就是你想要的。

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

例如,在此代码中,isMemberOfClass:将返回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

裁判:http://developer.apple.com/library/mac/文档/可可/引用/ /协议/ NSObject_Protocol /引用/ 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.

在nsdictionary中,必须为键设置字符串。我建议您使用NSStringFromClass([MyClass类])将类转换为字符串;把它当作钥匙。

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

使用类作为键的方式是不可能的。

#3


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");
        }

    }

#1


8  

Look up -isKindOfClass: and -isMemberOfClass:

查找-isKindOfClass:和-isMemberOfClass:

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

See the Apple isKindOfClass: documentation.

参见Apple isKindOfClass:文档。

#2


2  

- (BOOL)isMemberOfClass:(Class)aClass

this is what u seek probably.

这可能就是你想要的。

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

例如,在此代码中,isMemberOfClass:将返回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

裁判:http://developer.apple.com/library/mac/文档/可可/引用/ /协议/ NSObject_Protocol /引用/ 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.

在nsdictionary中,必须为键设置字符串。我建议您使用NSStringFromClass([MyClass类])将类转换为字符串;把它当作钥匙。

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

使用类作为键的方式是不可能的。

#3


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");
        }

    }