美文网首页
Framework中的struct

Framework中的struct

作者: 张宇航_Ken | 来源:发表于2016-08-12 21:42 被阅读74次

正常情况下,struct的用法是这样的:

struct Person {
    var name: String
    var age: Int
}

let person = Person(name: "Ken", age: 3)
print(person.name)

如果把Person的定义放到一个叫PeopleFramework中去,然后在调用的地方加上import语句来引用

import People

这样会报错:

Use of unresolved identifier 'Person'

因为Framework里的定义要声明为public才能给外部使用。如下:

public struct Person {
    var name: String
    var age: Int
}

但这样还是会报错:

'Person' cannot be constructed because it has no accessible initializers

因为一般情况下,struct在没有自定义init时,会自动生成一个全部成员的init,如下:

init(name: String, age: Int) {
    self.name = name
    self.age = age
}

但是一放到Framework中,怎么又提示"no accessible initializers"呢?
请看官方文档的描述:

"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."

也就是说这个自动生成的init的access level默认情况下是internal(如果有任何成员是private,则它也会降为private)。而像前面提到过的,标记为public的定义才能被Framework外部访问。所以自己写一个publicinit就好了。

不太理解为什么要这样设计,明白的同学还请指教。

代码变成这样:

public struct Person {
    var name: String
    var age: Int
    
    public init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}
let person = Person(name: "Ken", age: 11)
print(person.name)

但这样依然会报错:

Value of type 'Person' has no member 'name'

这是因为上面的Person虽然定义成了public,但成员还是internal的,要访问必须加上public。这个设计虽然稍显啰嗦,但还是比较严谨的。

最终代码:

public struct Person {
    public var name: String
    public var age: Int
    
    public init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
}
let person = Person(name: "Ken", age: 11)
print(person.name)

参考:
How can I make public by default the member-wise initialiser for structs in Swift?

相关文章

  • Framework中的struct

    正常情况下,struct的用法是这样的: 如果把Person的定义放到一个叫People的Framework中去,...

  • 数据库

    struct & class C++中的struct是对C中的struct的扩充。和class一样,struct有...

  • C++ 中的struct

    C++ 中也有struct, 而这个struct 和C中的struct不同,是功能扩展了的struct,当时的背景...

  • 十.Go结构struct

    结构struct Go中的struct与C中的struct相似,并且go没有class 使用type 结构名称 s...

  • 《日子》golang-结构struct

    结构struct -Go中的struct与C中的struct非常相似,并且Go没有class-使用type

  • 第五节结构struct

    struct相当于面向对象中的class 1.go中的struct与c中的struct非常相识,并且go没有cla...

  • Chapter 10 Structures and Unions

    struct中包含自己的情况 谨记以下要点: 不能在struct中包含自己, 否则struct的空间将无法分配 s...

  • go结构体和方法

    struct为何物 go中的struct可以实现oop中的类、方法。go语言中的struct成员可以是任何类型,如...

  • C语言中__packed 和位段的理解

    一:__packed typedef __packed struct struct常用在数据结构中。而struct...

  • hive使用struct、map与array类型字段

    hive支持struct,map,array三种集合类型 struct 与C语言、golang中的struct类似...

网友评论

      本文标题:Framework中的struct

      本文链接:https://www.haomeiwen.com/subject/qtwssttx.html