Swift 3中"public init(){}"的含义是什么?

Sea*_*404 0 swift

我是Swift的新手,我一直在努力学习如何在Swift 3中实现一个堆栈.在我发现的一些在线实现堆栈结构的参考代码中,我遇到了public init() {}.这是什么意思?

    public struct Stack<T> {
    private var elements = [T]()

    public init() {}

    public mutating func push(element: T) {
        self.elements.append(element)
    }

    public mutating func pop() -> T? {
        return self.elements.popLast()
    }

    public mutating func peek() -> T? {
        return self.elements.last
    }

    public func isEmpty() -> Bool {
        return self.elements.isEmpty
    }

    public func count() -> Int {
        return self.elements.count
    }
}
Run Code Online (Sandbox Code Playgroud)

Ish*_*ika 5

标记时public,事物在实现代码的框架之外可用,而init() {}swift初始化程序负责确保对象完全初始化.基本上,调用初始值设定项来创建特定类型的新实例.在最简单的形式中,初始化器就像没有参数的实例方法.

init() {
    // perform some initialization here
}
Run Code Online (Sandbox Code Playgroud)