How can I make the memberwise initialiser public, by default, for structs in Swift?
initialization, swift
Solution
Quoting the manual:
"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. Otherwise, the initializer has an access level of internal.
As with the default initializer above, if you want a public structure type to be initializable with a memberwise initializer when used in another module, you must provide a public memberwise initializer yourself as part of the type’s definition."
Excerpt from "The Swift Programming Language", section "Access Control".
Problem
I have a Swift framework that defines a struct: ``` public struct CollectionTO { var index: Order var title: String var description: String } ``` However, I can't seem to use the implicit memberwise initialiser from another project that imports the library. The error is: 'CollectionTO' cannot be initialised because it has no accessible initialisers i.e. the default synthesized memberwise initialiser is not `public`. ``` var collection1 = CollectionTO(index: 1, title: "New Releases", description: "All the new releases") ``` I'm having to add my own init method like so: ``` public struct CollectionTO { var index: Order var title: String var description: String public init(index: Order, title: String, description: String) { self.index = index; self.title = title; self.description = description; } } ``` ... but is there a way to do this without explicitly defining a `public init`?