You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 10:43:18