Objective c 如何在词典中检查类
在下面的字典中,我想为type类编写一个条件,有没有办法在iTreaction中单独识别类类型Objective c 如何在词典中检查类,objective-c,ios,Objective C,Ios,在下面的字典中,我想为type类编写一个条件,有没有办法在iTreaction中单独识别类类型 NSDictionary *dictionary = [NSDictionary dictionaryWithKeysAndObjects:@"check",@"checkValue",@"webservice", [webservice class], @"list",@"listValue", nil, @"task", [task class], @"new", @"newValue", @"o
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");
}
}
注意:我需要一个条件来检查值[webservice类]、[task类]、[operation类]
- (BOOL)isMemberOfClass:(Class)aClass
这可能就是你想要的
例如,在此代码中,isMemberOfClass:将返回NO:
NSMutableData *myData = [NSMutableData dataWithCapacity:30];
id anArchiver = [[NSArchiver alloc] initForWritingWithMutableData:myData];
if ([anArchiver isMemberOfClass:[NSCoder class]])
//something...
参考:
编辑: 在NSD字典中,您必须为键添加字符串。我建议您使用
NSStringFromClass([MyClass类])将该类转换为字符串编码>并将其作为键
要将类用作键的方式是不可能的。查找-isKindOfClass:和-isMemberOfClass:
if([object isKindOfClass:[AObjectClass class])
{
NSLog(@"object is of type AObjectClass");
}
请参见下面链接中找到的我的问题的答案
它是用来检查一个特定的类,我只想通用的解决方案来检查它是否是一个类alone@Nakkeeran:你想用这个类作为键的方式是不可能的,我建议用我答案中的方法作为键来解决这个问题。它用来检查一个特定的类,我只想用通用的解决方案来检查它是否是一个单独的类
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");
}
}