如何在默认情况下公开的成员明智的初始化结构在Swift?

前端之家收集整理的这篇文章主要介绍了如何在默认情况下公开的成员明智的初始化结构在Swift?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个swift框架定义一个结构:
public struct CollectionTO {
    var index: Order
    var title: String
    var description: String
}

但是,我似乎不能使用隐式成员明智的初始化从另一个项目,导入库。错误是’CollectionTO’不能初始化,因为它没有可访问的初始化。即它不给默认隐式成员明智的初始化公共关键字。

var collection1 = CollectionTO(index: 1,title: "New Releases",description: "All the new releases")

我必须添加我自己的init方法,如:

public struct CollectionTO {
    var index: Order
    var title: String
    var description: String

    public init(index: Order,title: String,description: String) {
        self.index = index;
        self.title = title;
        self.description = description;
    }

}

…但我宁愿不是有另一种方式任何人都知道吗?

我阅读手册:

“Default Memberwise Initializers for Structure Types
The default memberwise initializer for a structure type is considered private if any of the structure’s stored properties are private. Otherwise,the initializer has an access level of internal.

As with the default initializer above,if you want a public structure type to be initializable with a memberwise initializer when used in another module,you must provide a public memberwise initializer yourself as part of the type’s definition.”

摘录自“The Swift Programming Language”,第“Access Control”节。

原文链接:https://www.f2er.com/swift/321213.html

猜你在找的Swift相关文章