如何在Swift中创建类方法属性?

如何在Swift中创建类方法属性?,第1张

概述Objective-C中的类(或静态)方法是在声明中完成的。 @interface MyClass : NSObject+ (void)aClassMethod;- (void)anInstanceMethod;@end 如何在Swift中实现? 它们称为 type properties和 type methods,您使用类或静态关键字。 class Foo { var name 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 fooslet f = Foo()Foo.all.append(f)Foo.alert()       // There are 1 foos
总结

以上是内存溢出为你收集整理的如何在Swift中创建类方法/属性?全部内容,希望文章能够帮你解决如何在Swift中创建类方法/属性?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1060787.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存