Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

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

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!

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

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

over 4 years ago · Santiago Trujillo Denunciar

0

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)
 }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda