简体   繁体   English

在Swift中访问类中的静态变量

[英]Access static variables within class in Swift

Is ClassName.staticVaribale the only way to access static variable within the class? ClassName.staticVaribale是在类中访问静态变量的唯一方法吗? I want something like self , but for class. 我想要像self这样的东西,但是为了上课。 Like class.staticVariable . class.staticVariable

There are two ways to access a static property/method from a non-static property/method: 有两种方法可以从非静态属性/方法访问静态属性/方法:

  1. As stated in your question, you can prefix the property/method name with that of the type: 如您的问题中所述,您可以在属性/方法名称前加上类型的前缀:

     class MyClass { static let staticProperty = 0 func method() { print(MyClass.staticProperty) } } 
  2. Swift 2: You can use dynamicType : Swift 2:你可以使用dynamicType

     class MyClass { static let staticProperty = 0 func method() { print(self.dynamicType.staticProperty) } } 

    Swift 3: You can use type(of:) (thanks @Sea Coast of Tibet): Swift 3:你可以使用type(of:) (感谢@Sea Coast of Tibet):

     class MyClass { static let staticProperty = 0 func method() { print(type(of: self).staticProperty) } } 

If you're inside a static property/method you do not need to prefix the static property/method with anything: 如果你在静态属性/方法中,则不需要在静态属性/方法前加上任何东西:

class MyClass {
    static let staticProperty = 0

    static func staticMethod() {
        print(staticProperty)
    }
}

There is a way in Swift to make Marcel's answer satisfy even most picky style-guide gods 斯威夫特有一种方法可以让马塞尔的答案满足最挑剔的风格指导之神

class MyClass {

    private typealias `Self` = MyClass

    static let MyConst = 5

    func printConst() {
        print(Self.MyConst)
    }
}

That makes Self available like in protocols when you want access associated type declaration. 当你想要访问相关的类型声明时,这使得Self在协议中可用。 I am not sure about Swift 1 because never tried it but in Swift 2 it works perfectly 我不确定Swift 1,因为从未尝试过,但在Swift 2中它完美无缺

In a future Swift 3 version (yet to be released) you can use Self (yes, that's with a capital) to reference to the containing class. 在未来的Swift 3版本(尚未发布)中,您可以使用Self (是的,带有大写)来引用包含的类。 A proposal for this was accepted, but the feature is not implemented yet. 已接受提案,但该功能尚未实施。

For example: 例如:

struct CustomStruct {          
 static func staticMethod() { ... } 

 func instanceMethod() {          
   Self.staticMethod() // in the body of the type          
 }          
}

Source: https://github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md 资料来源: https//github.com/apple/swift-evolution/blob/master/proposals/0068-universal-self.md

You could work around this by defining a self referencing typealias. 您可以通过定义自引用类型来解决此问题。

class MyClassWithALongName {
  typealias CLASS = MyClassWithALongName

  static let staticFoo = "foo"

  func someInstanceMethod() -> String {
    return CLASS.staticFoo
  }
}

Though the style-guide gods may not approve. 虽然风格导游的神可能不赞成。

It looks like in Swift 4.2 the inner class and instance variable can directly access the static variable without the prefix of the class name. 看起来在Swift 4.2中,内部类和实例变量可以直接访问静态变量而不需要类名的前缀。 However, you still need the class name within a function. 但是,您仍需要函数中的类名。

class MyClass {
    static let staticProperty = 0
    let property = staticProperty //YES

    class Inner {
        func method() {
            print(staticProperty) //YES
        }
    }

    func method() {
        print(staticProperty) //NO
        print(MyClass.staticProperty) //YES
    }
}

I don't like the typealias way in this case. 我不喜欢这种情况下的typealias方式。 My workaround is: 我的解决方法是:

class MyClass {

   static let myStaticConst: Int = 1
   var myStaticConst:Int {
      return type(of: self).myStaticConst
   }

   func method() {
      let i:Int = myStaticConst
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM