Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 静态vs类作为类变量/方法(Swift)_Ios_Swift - Fatal编程技术网

Ios 静态vs类作为类变量/方法(Swift)

Ios 静态vs类作为类变量/方法(Swift),ios,swift,Ios,Swift,我知道static关键字用于在struct,enum等中声明类型变量/方法 但今天我发现它也可以用于类实体 class foo { static func hi() { println("hi") } class func hello() { println("hello") } } static关键字在class实体中的用途是什么 谢谢 编辑:我指的是Swift 1.2,如果它与Xcode 3 beta 3发行说明有任何区别的话: 类中现在允许使用“静态”方法

我知道
static
关键字用于在
struct
enum
等中声明类型变量/方法

但今天我发现它也可以用于
实体

class foo {
  static func hi() {
    println("hi")
  }
  class func hello() {
    println("hello")
  }
}
static
关键字在
class
实体中的用途是什么

谢谢


编辑:我指的是Swift 1.2,如果它与Xcode 3 beta 3发行说明有任何区别的话:

类中现在允许使用“静态”方法和属性(作为 “最终课程”的别名

因此,在Swift 1.2中,
hi()
定义为

class foo {
  static func hi() {
    println("hi")
  }
}
是类型方法(即对类型本身调用的方法)
它也是最终的(即不能在子类中重写)。

在类中,它用于完全相同的目的。然而,在Swift 1.2(目前为beta版)之前,
静态
不可用-备用的
说明符可用于声明静态方法和计算属性,而不是存储属性。

在Swift 5中,我使用
类型(of:self)
动态访问类属性:

class NetworkManager {
    private static var _maximumActiveRequests = 4
    class var maximumActiveRequests: Int {
        return _maximumActiveRequests
    }

    func printDebugData() {
        print("Maximum network requests: \(type(of: self).maximumActiveRequests).")
    }
}

class ThrottledNetworkManager: NetworkManager {
    private static var _maximumActiveRequests = 2
    override class var maximumActiveRequests: Int {
        return _maximumActiveRequests
    }
}

ThrottledNetworkManager().printDebugData()
印刷品2


在Swift 5.1中,我们应该能够使用大写字母S的
Self
,而不是
类型(of:)

在哪个版本的Xcode中尝试该代码??