Objective-C中的类(或静态)方法是在声明中完成的。
@interface MyClass : NSObject + (void)aClassMethod; - (void)anInstanceMethod; @end
如何在Swift中实现?
它们称为
type properties和
type methods,您使用类或静态关键字。
class Foo {
var name: String? // instance property
static var all = [Foo]() // static type property
class var comp: Int { // computed type property
return 42
}
class func alert() { // type method
print("There are \(all.count) foos")
}
}
Foo.alert() // There are 0 foos
let f = Foo()
Foo.all.append(f)
Foo.alert() // There are 1 foos