在我的旧 Obj-C 代码中,我可以声明一个字典,其值是Class
其他类的类型
NSMutableDictionary *Methods = [[NSMutableDictionary alloc] init];
[Methods setObject:[AuthReturnValue class] forKey:@"Authenticate"];
[Methods setObject:[MyOptions class] forKey:@"GetOptions"];
后来,根据键,我可以将它分配Class
给另一个变量
(在标题中)
Class returnType;
(在实施中):
returnType = (Class)[Methods objectForKey:methodName];
然后我可以使用这个Class
变量来声明一个相同类型的新变量(在这种情况下,它使用 JSONModel 并使用NSDictionary
来自其他地方的初始化它)
id<NSObject> result;
result = [[returnType alloc] initWithDictionary:(NSDictionary *)responseObject error:NULL];
这很好也很方便,因为 JSONModel 实现initWithDictionary
了,这意味着我可以通过Class
这种方式引入,而不必实例化特定类型。
我不知道如何在 Swift 中做到这一点。
例如,这不起作用:
var result: self.returnType.self()
var result: AnyClass = self.returnType.self
还有几十个变种。
如何在 Swift 中将变量声明为class
在对象中定义AnyClass
?还是我对这一切都错了?