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

Qiquan Lu picture Qiquan Lu · Feb 13, 2019 · Viewed 10.4k times · Source

I have a struct in a framework called "MyFramework"

public struct ShipmentPackage:Encodable {
  let package_code:String
  let weight:Float
}

Then when I try to create a ShipmentPackage in another project/framework

import MyFramework
let onePackage = ShipmentPackage(package_code:"BX",weight:100)

I got an error message 'ShipmentPackage' initializer is inaccessible due to 'internal' protection level I come to this link https://forums.swift.org/t/public-struct-init-is-unexpectedly-internal/5028

I tried to change my code to following:

1st attempt:

public struct ShipmentPackage:Encodable {
  let package_code:String
  let weight:Float
  public init(package_code:String,weight:Float){
    self.package_code = package_code
    self.weight = weight
  }
}

2nd attempt:

public struct ShipmentPackage:Encodable {
  public let package_code:String
  public let weight:Float
  public init(package_code:String,weight:Float){
    self.package_code = package_code
    self.weight = weight
  }
}

Also I tried to changing around the package_code and weight to public, but none of above works, I got error messages when compile

<unknown>:0: error: 'init' is inaccessible due to 'internal' protection level
<unknown>:0: note: 'init' declared here
<unknown>:0: error: 'init' is inaccessible due to 'internal' protection level

Any hint would be appreciated!

Answer

Andreas Oetjen picture Andreas Oetjen · Feb 13, 2019

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