Property Cannot Be Declared Public Because Its Type Uses an Internal Type

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

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.

Property cannot be declared open because its type uses an internal type (Typealias)

In this specific case (when used within a framework target), making the typealias public will not solve the issue. You need to use the platform condition check also when declaring the image property, as follows:

#if os(iOS)
import UIKit
typealias Image = UIImage
#elseif os(macOS)
import Cocoa
typealias Image = NSImage
#endif

public final class MyClass {

#if os(iOS)
public var image: UIImage
#elseif os(macOS)
public var image: NSImage
#endif

init?(imageURL: URL) {
guard let image = Image(contentsOf: imageFileURL) else {
return nil
}
self.image = image
}
}

The same applies for any public methods that use this type, whether it is a parameter or the return type of a function.

Offtopic: Make sure to initialize this class on a background queue/thread to avoid blocking the main thread and freezing the UI while the image is being downloaded.



Related Topics



Leave a reply



Submit