I’m working on a project in Xcode 16.2 and encountered an issue where getAPI() with a default implementation in a protocol extension doesn’t show up in autocomplete. Here’s a simplified version of the code:
import Foundation
public protocol Repository {
func getAPI(from url: String?)
}
extension Repository {
public func getAPI(from url: String? = "https://...") {
getAPI(from: url)
}
}
final class _Repository: Repository {
func getAPI(from url: String?) {
// Task...
}
}
let repo: Repository = _Repository()
repo.getAPI( // Autocomplete doesn't suggest getAPI()
I’ve tried the following without success: • Clean build folder • Restart Xcode • Reindexing
Is there something wrong with the code, or is this a known issue with Xcode 16.2? I’d appreciate any insights or suggestions.
Oh, right, I missed that subtlety. Thanks for the correction.
So, yeah, the behaviour in your first example is understandable but I think you could argue that Xcode should do better.
It’s understandable because this:
let repo: Repository = _Repository()
means that repo
is of type any Repository
, and only getAPI(from:)
is a requirement of the Repository
protocol. However, anything that conforms to Repository
will also gain the default argument version, so it’d be nice if Xcode included that. Feel free to file a bug about that. And if you do file a bug, please post your bug number.
Having said that, you can get what you want by changing the extension to this:
extension Repository {
public func getAPI() {
getAPI(from: "https://...")
}
}
Personally, I think that’s clearer in this case, but perhaps that’s not the case in your real sourcebase.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"