【发布时间】:2015-12-17 22:58:28
【问题描述】:
在 Objective-C 中我有一个类别:
@implementation NSObject (Entity)
+ (instancetype)entity{
Class cl = self.class;
NSObject *obj = [[cl alloc] init];
return obj;
}
@end
假设有 A 类和 B 类:
@interface A : NSObject
@end
@interface B : NSObject
@end
然后我可以使用静态entity 方法来创建对象,例如:
NSObject *o = [NSObject entity]; // type of NSObject
A *a = [A entity]; // type of A
B *b = [B entity]; // type of B
如何在 Swift 中编写该类别?我知道可以通过知道let s = NSString.self; var str = s.init() 之类的类型来创建对象,但在这里我不知道类,应该是self.self 之类的东西,它没有任何意义,而且肯定不能工作。
提前致谢
【问题讨论】:
-
This question 至少与您的相似。
标签: ios objective-c macos swift introspection