Strange code completion for a protocol method that has a default implementation with an associated value

Hey,

I have a simple setup in which I have a protocol with an associated type and a method that returns that associated type. I also have a default implementation for the method.

In my struct that conforms to the protocol, I want to override that default implementation.
When I start typing the name of the method someView(), Xcode suggests a code-completion. But that code-completion uses the name of the associated type as the return type, which does not compile.

Without a default implementation of the method, Xcode puts some View as the return type, which is what I would consider correct :thinking:

Is this expected behavior? And is there a way to prevent this from happening? I don't like the thought that every time I try to add this method, I have to fix the return type manually.

Here's the example code:

import SwiftUI

protocol MyProtocol {
    associatedtype Content: View
    @ViewBuilder func someView() -> Content
}

extension MyProtocol {
    // Provide a default
    func someView() -> some View {
        Text("ABC")
    }
}

struct MyStruct: MyProtocol {
    // This is what Xcode code-completion suggests. 
    // It inserts `Content` as a return type instead of `some View`. 
    // This does not compile and we have to manually replace it every time we add this method. 
    // Note that this does not happen, when there is no default implementation.
    func someView() -> Content {
        Color.red
    }
}

Thanks a lot for any help and happy holidays :slight_smile:

1 Like

A tiny note: The extension contains not a default implementation. It's an overload.

2 Likes