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

Swift

Swift Problem Overview


I created two classes Content and Bucket. Bucket contains an array of Content objects and exposes that via a public property. However, when I do so, I receive the error:

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

enter image description here

Any thoughts on why this is raising an error?

Swift Solutions


Solution 1 - Swift

You have to declare the access level of the Content class public as well.

public class Content {
   // some code
}

As stated in the 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.

Classes are declared as internal by default, so you have to add the public keyword to make them public.

A similar rule exists for functions as well.

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

Solution 2 - Swift

Content must be declared as public too:

public class Content {
  …
}

Depending on your use-case you might declare Bucket as internal, too. Just omit the public keyword in this case.

Solution 3 - Swift

My issue was a namespace problem.

I had declared an enum called Data and that was mucking with the Swift Data class, especially an imageData: Data property within a Core Data model.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSteveView Question on Stackoverflow
Solution 1 - SwiftCihan TekView Answer on Stackoverflow
Solution 2 - SwiftKoraktorView Answer on Stackoverflow
Solution 3 - SwiftpkambView Answer on Stackoverflow