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

christophriepe

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?

Joakim Danielson

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Codable class does not conform to protocol Decodable

How to make a class conform to a protocol in Swift?

UIViewControllerRepresentable - "Protocol cannot be satisfied by a non-final class" - SwiftUI

How to make Xcode automatically conform to a protocol

Make swift class conform to objective c protocol that defines properties

Method in non-final class must return `Self` to conform to protocol

xcode 7 Objective c Warning - Null passed to a callee that requires a non-null argument

Protocol requirement cannot be satisfied by a non-final class because it uses 'Self' in a non-parameter, non-result type

Self-type does not conform to base class

Class does not conform to protocol. Why?

Swift class doesn't conform to objective c protocol inherited from swift protocol

How to make a swift class conform to a protocol that requires a protocol-conform type for one of its properties?

Class does not conform to protocol, but struct does

Value of protocol type 'Encodable' cannot conform to 'Encodable'; only struct/enum/class types can conform to protocols

How to make Swift class conform to Objective-C protocol, without exposing it to Objective-C?

Why Local Inner class requires local variable Final? This Variable is in same method in which Local Inner class define

Derived class doesn't conform to protocol

Swift class does not conform to Objective-C protocol with error handling

Class does not conform to protocol

Conform class extension to generic protocol function

Cannot get data from "self" keyword from a class which is extended from a protocol in swift iOS?

Swift class does not conform to Protocol with Enum

Class conform to protocol with function that receives a generic parameter

Return Class conform to protocol

Codable class doesn't conform to protocol 'Decodable'

Xcode 13.3 warning: "'self' refers to the method '{object}.self', which may be unexpected

Protocol 'Any' as a type cannot conform to 'Hashable'

Type 'any Protocol' cannot conform to 'Protocol'

Swift - Type 'any Protocol' cannot conform to 'Protocol'