【发布时间】:2015-09-08 18:11:39
【问题描述】:
我想要一个通用协议来返回给定类的新“随机”配置实例。
在 ObjC 中:
@protocol Random
+ (instancetype)random;
@end
@interface UIColor (Random)
<Random>
@end
@implementation
+ (instancetype)random {
return [UIColor colorWith...];
}
@end
它在 ObjC 中工作,但我无法让它在 Swift 中工作。
在 Swift 中:
protocol Random {
static func randomExample() -> Self
}
extension UIColor: Random {
final class func randomExample() -> UIColor {
return UIColor(red: ...)
}
}
但是无论我如何配置它都会引发错误。
如何正确地为返回符合类实例的类方法制定协议?
【问题讨论】:
-
它会抛出什么错误?