Thursday, September 28, 2023
HomeiOS Developmentios - Making `Identifiable` protocol backwards suitable

ios – Making `Identifiable` protocol backwards suitable

[ad_1]

Problem

I really hate that apple made Identifiable only available in iOS 13+. It’s a super useful protocol. So I end up using my own “Identifiable” protocol named CompatibilityIdentifiable.

public protocol CompatibilityIdentifiable {
    associatedtype Id: Equatable
    var id: Id { get }
}

Question:

Is there any way to make my custom CompatibilityIdentifiable also conform to Identifiable on iOS 13+?

Background:

In a regular project this has no use. But in an SPM package it means a lot because I can implement the same behaviour for CompatibilityIdentifiable as for Identifiable without duplicating my code.

Here is an example:

extension TableViewHelper where Section: CompatibilityIdentifiable {
    public func replace(sections: [Section], with animation: UITableView.RowAnimation = .automatic) {
        replaceSections(with: animation) { oldSection in
            return sections.first(where: { $0.id == oldSection.id })
        }
    }
}

If I can simply make CompatibilityIdentifiable conform to Identifiable I can use Identifiable for projects that are iOS 13+

I honestly don’t think this is possible unless I simply copy and paste the code like this:

@available(iOS 13, *)
extension TableViewHelper where Section: Identifiable {
    public func replace(sections: [Section], with animation: UITableView.RowAnimation = .automatic) {
        replaceSections(with: animation) { oldSection in
            return sections.first(where: { $0.id == oldSection.id })
        }
    }
}

I won’t be doing this as we’re talking about a lot of functions. So, if there is no way, I will continue using CompatibilityIdentifiable for my future projects and my SPMs

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments