CoreStore 9.2.0中遵循FieldRelationshipType协议的步骤及报错解决
解决CoreStore 9.2.0中@Field.Relationship的编译错误(Swift 5.10)
修正后的正确代码
import Foundation import CoreStore class MICSelecta: CoreStoreObject { @Field.Relationship("performances", inverse: \.$selecta) var performances: Set<MICPerformance> } class MICPerformance: CoreStoreObject { @Field.Relationship("selecta", inverse: \.$performances) var selecta: MICSelecta }
错误原因与解决步骤
1. 移除多余的手动协议扩展
你手动添加的FieldRelationshipToManyType和FieldRelationshipToOneType扩展完全是多余的。CoreStoreObject的子类会通过@Field.Relationship属性自动合成这些协议的一致性实现,手动实现反而会引发类型冲突,导致编译错误。直接删除这两个扩展即可。
2. 修正反向关联的语法
原代码中\MICPerformance.$selecta的写法会导致泛型参数推断失败,CoreStore 9.x在Swift 5.10中支持更简洁的反向关联语法:直接使用\.$propertyName(美元符号+属性名),不需要指定类名,上下文会自动关联对应的对象类型。
3. 为一对一关联显式指定反向关联
MICPerformance中的一对一关联必须显式声明反向关联参数inverse: \.$performances,否则CoreStore无法正确建立双向关系映射,进而触发协议一致性检查错误。
对应错误的解决说明
Generic parameter 'D' could not be inferred:通过修正反向关联语法为\.$selecta解决。Generic struct 'Relationship' requires that 'MICSelecta' conform to 'FieldRelationshipType'/requires that 'MICSelecta' conform to 'FieldRelationshipToOneType':删除手动添加的协议扩展,并为一对一关联补充反向关联参数后,CoreStore会自动生成正确的协议一致性代码,错误会自动消失。
内容的提问来源于stack exchange,提问作者schwabsauce
相关产品推荐
相关产品推荐

