2

I have

protocol ErrorContent { var descriptionLabelText: String { get set } } extension ErrorContent { var descriptionLabelText: String { return "Hi" } } struct LoginErrorContent: ErrorContent { var descriptionLabelText: String init(error: ApiError) { ... } } 

and xcode is complaining that "Return from initializer without initializing all stored properties." What I want here is to just use the default value that I gave the descriptionLabelText in the protocol extension. Isn't that the point of protocol extensions? Anyways I'd like to understand why this is wrong and what I can do to use my default value.

1 Answer 1

3

Almost correct, just a couple of issues with your code:

  1. You don't need to declare the variable in LoginErrorContent, as the implementation is already in the ErrorContent extension. Declaring it again overrides the extension implementation

  2. If you want to use the extension computed property for descriptionLabelText, you can't specify that it is a setter, as it only returns a value.

Example:

protocol ErrorContent { var descriptionLabelText: String { get } } extension ErrorContent { var descriptionLabelText: String { return "Hi" } } struct LoginErrorContent: ErrorContent { // Overriding the extension behaviour var descriptionLabelText: String { return "Hello" } init(error: ApiError) { ... } } 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. What if I want to conditionally override the descriptionLabelText?
I have edited my example to show how to override the extension's implementation - adding same implementation in struct with different return value.
is there a way to conditionally override? @Yasir
I don't think there is any way of calling back up to the extension's implementation, so you cannot have the overriding be conditional. You would have the different implementations in separate variables then have the conditional logic choose which one to use.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.