声明私有(private) var 时出现 Swift 结构编译器错误

标签 swift struct xcode12

我有一个很简单的struct按预期工作:

struct Obligation {
    
    var date = Date()
}

let snapshotEntry = Obligation(date: Date())
但是,如果我向这个结构添加一个私有(private)变量,我会在我创建我的结构的一个实例的行上得到一个编译错误,说 Argument passed to call that takes no arguments :
struct Obligation {
    
    var date = Date()
    
    private var blank:Bool = false
}

let snapshotEntry = Obligation(date: Date())
enter image description here
如果我删除 private来自新 blank var 它编译得很好。
我在这里忽略了一些简单的事情吗?可以一个struct没有私有(private)变量?

最佳答案

作为Access Control文件明确指出:

The default memberwise initializer for a structure type is considered private if any of the structure’s stored properties are private. Likewise, if any of the structure’s stored properties are file private, the initializer is file private. Otherwise, the initializer has an access level of internal.


只需使用自定义初始化程序。

关于声明私有(private) var 时出现 Swift 结构编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63645914/

相关文章:

c++ - 如何在 C++ 代码中使用 C 结构?

ios - 使用空的 LLDB 目标可能会导致从远程设备读取内存缓慢

swift - 使用 UISlider 的起始值问题

swift - Assets 单例和引用循环

ios - 如何使用 segues 执行自定义动画?

ios - 不同类中的 Swift 对象不更新

swift - Xcode 12 Interface Builder 显示 “No selection”

ios - 火存储 : How to get the object data from firestore in IOS (Swift) and display it in tableview

C++ StackOverflowException 初始化超过 63992 的结构

c++ - 带有 list<..> 的结构在 2 dim 中。删除时的动态数组段错误