Doubts about Swift initializers [closed]

I know this sounds like a silly question for all of you but I wanted to have more clarity on the differences of using init() or not in a structure

I meant that whether I use init() or not the parameters are still required. That's why I was wondering the difference of using init() or simple variables.


1^ Example without init()

struct ProductsImageView: View {
    
     var image: String
     var title: String
     var time: Int
     var isAllInOne: Bool = false

    
    var body: some View {

   }
} 

2^ Example with init()

struct ProductsImageView: View {
    
     var image: String
     var title: String
     var time: Int
     var isAllInOne: Bool
    
    init(image: String, title: String, time: Int, isAllInOne: Bool = false) {
        self.image = image
        self.title = title
        self.time = time
        self.isAllInOne = isAllInOne
    }
    
    var body: some View {
  }
}

In both cases the various parameters will still be required when we call a structure in the code

ProductsImageView(image: "slider3", title: "Lorem", time: 60, isAllInOne: true)
  • Now I wanted to know when is it right to use init() and when not?

  • What are the differences?

Excuse me again for the stupid question but I prefer to have clear what I learn often I have some doubts and I ask you


If you don't write an init in the struct declaration, Swift will synthesize one for you. The init you wrote in example 2 is exactly the same as what Swift synthesizes in example 1.

However, the visibility of the synthesized init is always internal, even if the struct is public. So when you're creating a public struct and you want its init to be visible in other modules, you must write out its public init explicitly.