【问题标题】:How do I make class methods / properties in Swift?如何在 Swift 中创建类方法/属性?
【发布时间】:2014-06-06 17:54:46
【问题描述】:

Objective-C 中的类(或静态)方法是在声明中使用 + 完成的。

@interface MyClass : NSObject

+ (void)aClassMethod;
- (void)anInstanceMethod;

@end

如何在 Swift 中实现这一点?

【问题讨论】:

    标签: swift


    【解决方案1】:

    它们被称为type propertiestype methods,您使用classstatic 关键字。

    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
    

    【讨论】:

    • 我不认为它仅限于 Playground,它也不能在应用程序中编译。
    • @ErikKerber 很高兴知道,还不需要它们所以没有测试自己,谢谢。
    • Xcode 6.2 仍会针对“class var varName:Type”形式的任何内容报告“尚未支持的类变量”。
    • 在 Swift 2.0+ 中,您不需要在函数或计算类型属性之前使用 class 关键字。
    【解决方案2】:

    它们在 Swift 中被称为类型属性和类型方法,您可以使用 class 关键字。
    在 swift 中声明一个类方法或类型方法:

    class SomeClass 
    {
         class func someTypeMethod() 
         {
              // type method implementation goes here
         }
    }
    

    访问该方法:

    SomeClass.someTypeMethod()
    

    或者你可以参考Methods in swift

    【讨论】:

    • 非常感谢!它甚至比 Objective-C 中的 NSObject 类更容易,而且设置起来已经很容易了。
    【解决方案3】:

    如果是类,则在声明前添加class,如果是结构,则在声明前添加static

    class MyClass : {
    
        class func aClassMethod() { ... }
        func anInstanceMethod()  { ... }
    }
    

    【讨论】:

    • 这里不需要func 关键字吗?
    • 当然。让我站在拥挤的公共汽车上回答问题,哈哈。已更正。
    • 简洁的答案。一代 Objective-C 代码可能没有被移植的一个被忽视的方面是下一个人可能不得不将它移植到某个版本的 Swift。这很有帮助,因为我完全忘记了来自 Obj-C 的类方法的概念,而且我正在移植一些混乱。
    【解决方案4】:

    Swift 1.1 没有存储类属性。您可以使用一个闭包类属性来实现它,该属性获取与类对象相关联的关联对象。 (仅适用于从 NSObject 派生的类。)

    private var fooPropertyKey: Int = 0  // value is unimportant; we use var's address
    
    class YourClass: SomeSubclassOfNSObject {
    
        class var foo: FooType? {  // Swift 1.1 doesn't have stored class properties; change when supported
            get {
                return objc_getAssociatedObject(self, &fooPropertyKey) as FooType?
            }
            set {
                objc_setAssociatedObject(self, &fooPropertyKey, newValue, objc_AssociationPolicy(OBJC_ASSOCIATION_RETAIN_NONATOMIC))
            }
        }
    
        ....
    }
    

    【讨论】:

    • 我一直在学习 Swift,想知道您是否可以将关联的对象附加到 Swift 类实例。听起来答案是“有点”。 (是的,但仅限于 NSObject 子类的对象。)感谢您为我解决了这个问题。 (已投票)
    【解决方案5】:

    如果是函数,则在声明前加上 class 或 static,如果是属性,则在声明前加上 static。

    class MyClass {
    
        class func aClassMethod() { ... }
        static func anInstanceMethod()  { ... }
        static var myArray : [String] = []
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-03
      • 2021-12-17
      • 2011-04-06
      • 1970-01-01
      • 2021-02-08
      • 2021-07-14
      • 2016-03-08
      • 1970-01-01
      • 2016-12-26
      相关资源
      最近更新 更多