Has this happened to you? You write code and the compile warns you that a method “nearly matches” an “optional requirement”.
Let me tell you why this happens and what you should be looking for.
First, some sample code:
public class Foo: UIViewController { } extension Foo { public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 1 } public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { return UITableViewCell() } }
This compiles. No warning. If you add the missing UITableViewDataSource
conformance declaration, it still compiles without warnings. But if you add conformance to UITableViewDelegate
to the extension without conforming to data source, you get this odd error message.
That’s because the “near miss” detector code sees the delegate conformance, and tries to match the method declarations to the known conformance. Both methods here are public, there’s an overlap in the naming pattern, and the detector assumes you’re most likely trying to conform to the protocol named in the extension.
If in fact, you’ve forgotten a conformance, just add it in. Declaring both data source and delegate fixes this issue. If you’re encountering a weird accidental overlap of names, you can:
- rename the method so it doesn’t trigger the detector
- move the method to a different extension
- mark the method private, so it’s clear that you’re not using it for conformance
Of course, if you really did mess up the name and it’s not an accidental overlap, you can just fix your error.
Comments are closed.