如何在Swift中创建类方法/属性?
发布时间:2020-12-14 06:11:52 所属栏目:百科 来源:网络整理
导读:Objective-C中的类(或静态)方法是在声明中完成的。 @interface MyClass : NSObject+ (void)aClassMethod;- (void)anInstanceMethod;@end 如何在Swift中实现? 它们称为 type properties 和 type methods ,您使用类或静态关键字。 class Foo { var name: Str
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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |