swift – 属性不能被声明为public,因为它的类型使用内部类型

前端之家收集整理的这篇文章主要介绍了swift – 属性不能被声明为public,因为它的类型使用内部类型前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我创建了两个类Content和Bucket。 Bucket包含一组Content对象,并通过public属性公开。但是,当我这样做,我收到错误

Property cannot be declared public because its type uses an internal type

对于为什么会引发错误的任何想法?

您还必须声明Content类的访问级别public。
public class Content {
   // some code
}

documentation所述:

A public variable cannot be defined as having an internal or private
type,because the type might not be available everywhere that the
public variable is used.

默认情况下,类被声明为内部,因此您必须添加public关键字才能使其公开。

函数也存在类似的规则。

A function cannot have a higher access level than its parameter types and return type,because the function could be used in situations where its constituent types are not available to the surrounding code.

猜你在找的Swift相关文章