public struct in framework init is inaccessible due to 'internal' protection level in compiler public struct in framework init is inaccessible due to 'internal' protection level in compiler swift swift

public struct in framework init is inaccessible due to 'internal' protection level in compiler


Lesson learned: all public struct need a public init

That's not quite exact. The documentation states:

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. Likewise, if any of the structure’s stored properties are file private, the initializer is file private. Otherwise, the initializer has an access level of internal.

So the build-in memberwise initializer is only available within the package. If you don't provide a public initializer, you won't be able to create your struct from outer space.

public struct YourFrameworkStruct {    let frameworkStructProperty: String!    /// Your public structs in your framework need a public init.    ///     /// Don't forget to add your let properties initial values too.    public init(frameworkStructProperty: String) {        self.frameworkStructProperty = frameworkStructProperty    }}


Thanks for all the comments,I finally figured out why it's giving me error.Both my 2 attempts should be fine.

And it turned out this struct wasn't causing issues

I have other struct use this struct and marked public,for example

public struct Shipment:Encodable {  let carrier_code:String  let packages:[ShipmentPackage]}

was missing initializer, but for whatever reason XCode won't indicate the error for my workspace, but giving out error at compile time.

after giving an initializer to all public structs, the app pass the compiler.

public struct Shipment:Encodable {  let carrier_code:String  let packages:[ShipmentPackage]  public init(carrier_code:String,packages:[ShipmentPackage]){      self.carrier_code = carrier_code      self.packages = packages  }}

My original post wasn't really good, since there was nothing wrong with the code I posted, but decide to not delete this post, it might help newbies like me in future

Lesson learned: all public struct need a public init


When you are trying to access a struct in a different framework,**

all the variables within the struct should be public and should have a default value set in the init

**

eg:

public struct LocationPoint { public var location: NSNumber public var color: UIColor

    public init(location: NSNumber? = 0, color: UIColor? = nil ) {        self.location = location ?? 0        self.color = color ?? UIColor.init(red: 255.0/255.0, green: 1.0, blue: 205/255.0, alpha: 1)    }}

You can also write the init as below depending on the way you call the struct.

 public init() {     self.location = 0     self.color = UIColor.init(red: 255.0/255.0, green: 1.0, blue: 205/255.0, alpha: 1) }