Home > Software engineering >  Xcode Warning: Non-final class 'CustomMedication' cannot safely conform to protocol '
Xcode Warning: Non-final class 'CustomMedication' cannot safely conform to protocol '

Time:09-17

Context

I am currently working with Protocols and AssociatedTypes and encountered the following new Xcode Warning I don't really understand.

Non-final class 'SomeCustomComponent' cannot safely conform to protocol 'CustomComponent', which requires that 'Self.C.CC' is exactly equal to 'Self'; this is an error in Swift 6


Code

protocol Component {
    associatedtype CC: CustomComponent where CC.C == Self

    var customComponent: CC { get }
}

protocol CustomComponent {
    associatedtype C: Component where C.CC == Self

    var component: C { get }
}

enum SomeComponent: Component {
    var customComponent: { ... }
}

// Please note, that SomeCustomComponent is an NSManagedObject conforming to CustomComponent.
extension SomeCustomComponent: CustomComponent { // I get the Xcode Warning in this Line.
    var component: C { ... }
}

Question

How can I handle this Xcode Warning? Is it possible to mark an NSManagedObject as final? And how do I do it since it is defined in the background?

CodePudding user response:

You can mark an NSManagedObject subclass as final but this of course requires that you manage the code for the subclass yourself, that is set Codegen to Manual for this entity in your core data model.

Regarding the final requirement, this is because if you would create a subclass the mapping between the two protocols might break.

Consider if you create a subclass of SomeCustomComponent,

class SubCustomComponent: SomeCustomComponent {}

with no other changes that concern the protocols. Then the component property of the subclass would point to the enum SomeComponent but the enum would not point back to the new class SubCustomComponent but to its superclass.

So if you would use this for some kind of conversion or mapping in both directions your would go from SubCustomComponent to SomeComponent and back to SomeCustomComponent which is surely a bug.

  • Related