【发布时间】:2014-12-08 12:44:19
【问题描述】:
是否可以在 swift 的扩展中定义类函数,就像在 Objective-C 类别中也可以定义类函数一样?
objective-c 中的示例
@implementation UIColor (Additions)
+ (UIColor)colorWithHexString:(NSString *)hexString
{
// create color from string
// ... some code
return newColor;
}
@end
swift 中的等价物是什么?
【问题讨论】:
-
只要不使用实例变量/计算属性,我们难道不总是将函数用作类函数吗?
-
参见this SO answer 了解如何创建 Swift 扩展。