50

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!

Marcy
  • 4,611
  • 2
  • 34
  • 52
Qiquan Lu
  • 615
  • 1
  • 5
  • 10
  • 1
    Show how are you calling this initializer you wrote – mag_zbc Feb 13 '19 at 15:06
  • come across this link as well, http://taylorfranklin.me/2016/07/10/the-default-memberwise-initializers-headache/, which pretty much exact same situation as mine, but doesn't work for me, since it shows error at compile time, do I need do anything for compile setting? – Qiquan Lu Feb 13 '19 at 15:07
  • Cannot reproduce. I have copied your “1st attempt” into a framework, and the initializer can be called without problems from the main application. – Martin R Feb 13 '19 at 15:23
  • 1
    @MartinR thanks, I figured out the issue, it wasn't caused by this struct, but other one using this one doesn't have a public initializer. – Qiquan Lu Feb 13 '19 at 17:02

3 Answers3

75

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
    }
}
App Dev Guy
  • 5,396
  • 4
  • 31
  • 54
Andreas Oetjen
  • 9,889
  • 1
  • 24
  • 34
6

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

Qiquan Lu
  • 615
  • 1
  • 5
  • 10
1

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)
 }
Abdulrahman Alsoghayer
  • 16,462
  • 7
  • 51
  • 56