简体   繁体   中英

Xcode Warning: Non-final class 'CustomMedication' cannot safely conform to protocol 'CustomComponent', which requires that 'Self.C.CC' is exactly

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM