Kevin
Kevin

Reputation: 1189

Swift Pass Multiple Parameters through Init

I have a class with two initializers that I'm interested in passing information, title and description.

class CustomSearchResult {

    var description: String?
    var title: String?
    init(title: String) {
        self.title = title
    }
    init(description: String) {
        self.description = description
    }

}

Which later I reference in order to store this information into a custom search

customSearchResults.append(SearchResultType.customResult(result: CustomSearchResult(title: titleString!,description: descriptionString!)))

However, this produces an error "Cannot invoke initializer for type 'CustomSearchResult' with an argument list of type '(title: String, description: String)'"

Is there a way to pass both these initialized variables as described?

Upvotes: 0

Views: 3702

Answers (1)

Michael Bernat
Michael Bernat

Reputation: 646

Create another initializer:

init(title: String?, description: String?) {
        self.title = title
        self.description = description
}

You may not need to use optionals as parameters, according to your needs...

init(title: String, description: String) {
        self.title = title
        self.description = description
}

Upvotes: 2

Related Questions