我想将一个Class对象存储在NSMutableDictionary中,然后根据该键(特别是NSNotification名称)实例化一个副本.但不幸的是,我无法找到Objective-C中"Class"类型的对象,而且很难google.它绝对不是'id'类型所以它通常不会进入字典:
warning: Semantic Issue: Incompatible pointer types sending 'Class *' to parameter of type 'id'
Run Code Online (Sandbox Code Playgroud)
或者,我可以将Class的名称存储在字典中,并使用NSClassFromString来实例化该类.使用NSStringFromClass将Class放入Dictionary中,然后再使用NSClassFromString来获取类似乎很愚蠢.这是我最好的选择吗?
您可以将其存储Class到字典中,例如
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
Class k = [NSString class];
[dict setObject:k forKey:@"foo"];
NSLog(@"%@", dict);
Run Code Online (Sandbox Code Playgroud)
实际上,一个Class是兼容的id.请注意,您不要需要一个*后Class.就像id.