加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 06:07:31 所属栏目:百科 来源:网络整理
导读:我有一个swift框架定义一个结构: public struct CollectionTO { var index: Order var title: String var description: String} 但是,我似乎不能使用隐式成员明智的初始化从另一个项目,导入库。错误是’CollectionTO’不能初始化,因为它没有可访问的初始
我有一个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”节。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读