How can I make the memberwise initialiser public, by default, for structs in Swift?

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?

3 Answers
3

Leave a Comment