Type 'CounterModel' does not conform to protocol 'PersistentModel` – SwiftData

huangapple go评论99阅读模式
英文:

Type 'CounterModel' does not conform to protocol 'PersistentModel` - SwiftData

问题

I am trying to make a sample app which uses SwiftData to keep track of sports goals. I have a struct: Team which has values id (UUID), name (string), score (int), and editing (bool). I have a SwiftData model class which contains the values id (UUID), teams ([Team]), pro (Boolean). Normally, the class works fine without having the teams array in the CounterModel class, but as soon as I add var teams: [Team] to the class, I get the following errors:

  • Type 'CounterModel' does not conform to protocol 'PersistentModel'
  • No exact matches in call to instance method 'getValue'
  • No exact matches in call to instance method 'setValue'

The class conforms to Identifiable, Hashable. The struct also conforms to these protocols. Conforming both to Codable does not make the error go away.

Here is my code if needed:

import Foundation
import SwiftData

@Model
class CounterModel: Identifiable, Hashable {
    @Attribute(.unique) var id = UUID()
    var teams: [Team]
    var pro: Bool
    
    init(teams: [Team], pro: Bool) {
        self.teams = teams
        self.pro = pro
    }
    func toTeamArr() -> [Team] {
        return teams
    }
}

struct Team: Identifiable, Hashable {
    var id: UUID = UUID()
    var name: String
    var score: Int
    var editing: Bool = false
}

I have also tried putting the Team struct inside of the class. Same error.

Following a suggestion by a reply, here is the updated code:

import Foundation
import SwiftData

@Model
class CounterModel: Identifiable, Hashable {
    @Attribute(.unique) var id = UUID()
    @Relationship(.cascade) var teams: [Team]
    var pro: Bool
    
    init(teams: [Team], pro: Bool) {
        self.teams = teams
        self.pro = pro
    }
    func toTeamArr() -> [Team] {
        return teams
    }
}

@Model
class Team: Identifiable, Hashable {
    @Attribute(.unique) var id: UUID = UUID()
    var name: String
    var score: Int
    @Transient var editing: Bool = false
    
    init(name: String, score: Int, editing: Bool = false) {
        self.name = name
        self.score = score
        self.editing = editing
    }
}

Now, there are only 2 errors produced:

  • Type 'CounterModel' does not conform to protocol 'PersistentModel'
  • Type 'Team' does not conform to protocol 'PersistentModel'

Upon extracting the models into a separate Xcode project, it builds fine. When I bring the rest of my code into the project, it stops working. Will investigate further.

Okay. It seems to be an issue with passing the data from the @Query modifier into different views. Upon removing the parameters for the CounterModel data model from the views, the app compiled fine. I'm assuming, and hoping that this is a bug. For now, I'll just query the data separately in the views. Thank you all for your help! I have submitted feedback on the issue, just in case: FB12338703

英文:

I am trying to make a sample app which uses SwiftData to keep track of sports goals. I have a struct: Team which has values id (UUID), name (string), score (int), and editing (bool). I have a SwiftData model class which contains the values id (UUID), teams ([Team]), pro (Boolean). Normally, the class works fine without having the teams array in the CounterModel class, but as soon as I add var teams: [Team] to the class, I get the following errors:

  • Type 'CounterModel' does not conform to protocol 'PersistentModel'
  • No exact matches in call to instance method 'getValue'
  • No exact matches in call to instance method 'setValue'

<br>
The class conforms to Identifiable, Hashable. The struct also conforms to these protocols. Conforming both to Codable does not make the error go away.
Here is my code if needed:

import Foundation
import SwiftData

@Model
class CounterModel: Identifiable, Hashable {
    @Attribute(.unique) var id = UUID()
    var teams: [Team]
    var pro: Bool
    
    init(teams: [Team], pro: Bool) {
        self.teams = teams
        self.pro = pro
    }
    func toTeamArr() -&gt; [Team] {
        return teams
    }
    
}
struct Team: Identifiable, Hashable {
    var id: UUID = UUID()
    var name: String
    var score: Int
    var editing: Bool = false
}

I have also tried putting the Team struct inside of the class. Same error.

Following a suggestion by a reply, Here is the updated code:

import Foundation
import SwiftData

@Model
class CounterModel: Identifiable, Hashable {
    @Attribute(.unique) var id = UUID()
    @Relationship(.cascade) var teams: [Team]
    var pro: Bool
    
    init(teams: [Team], pro: Bool) {
        self.teams = teams
        self.pro = pro
    }
    func toTeamArr() -&gt; [Team] {
        return teams
    }
}

@Model
class Team: Identifiable, Hashable {
    @Attribute(.unique) var id: UUID = UUID()
    var name: String
    var score: Int
    @Transient var editing: Bool = false
    
    init(name: String, score: Int, editing: Bool = false) {
        self.name = name
        self.score = score
        self.editing = editing
    }
}

Now, there are only 2 errors produced:

  • Type 'CounterModel' does not conform to protocol 'PersistentModel'
  • Type 'Team' does not conform to protocol 'PersistentModel'

Upon extracting the models into a separate Xcode project, it builds fine. When I bring the rest of my code into the project, it stops working. Will investigate further.

Okay. It seems to be an issue with passing the data from the @Query modifier into different views. Upon removing the parameters for the CounterModel data model from the views, the app compiled fine. I'm assuming, and hoping that this is a bug. For now, I'll just query the data separately in the views. Thank you all for your help! I have submitted feedback on the issue, just in case: FB12338703

答案1

得分: 3

好的。似乎存在将数据从 @Query 修饰符传递到不同视图的问题。在从视图中删除 CounterModel 数据模型的参数后,应用程序编译正常。我假设,并希望这是一个错误。目前,我将在视图中分别查询数据。感谢大家的帮助!

英文:

Okay. It seems to be an issue with passing the data from the @Query modifier into different views. Upon removing the parameters for the CounterModel data model from the views, the app compiled fine. I'm assuming, and hoping that this is a bug. For now, I'll just query the data separately in the views. Thank you all for your help!

答案2

得分: 0

有两个更改需要对你的模型类进行:

第一个更改是将 Team 改为一个类,并将其标记为 @Model
第二个更改是告诉 SwiftData,teams 是对另一个 SwiftData 实体的引用,使用 @Relationship 装饰器。

class CounterModel: Identifiable, Hashable {
    @Attribute(.unique) var id = UUID()
    @Relationship(.cascade) var teams: [Team]
    var pro: Bool
    
    init(teams: [Team], pro: Bool) {
        self.teams = teams
        self.pro = pro
    }
    func toTeamArr() -> [Team] {
        return teams
    }
    
}

@Model
class Team: Identifiable, Hashable {
    @Attribute(.unique) var id: UUID = UUID()
    var name: String
    var score: Int
    @Transient var editing: Bool = false
}

@Relationship 中的 .cascade 参数告诉 SwiftData,如果删除 CounterModel 对象,将删除所有引用的 Team 对象。

英文:

There are two changes you need to make to your model classes.

The first, is to change Team to a class and make it an @Model
The second change is to tell SwiftData that teams is a reference to another SwiftData entity using the @Relationship decorator.

class CounterModel: Identifiable, Hashable {
    @Attribute(.unique) var id = UUID()
    @Relationship(.cascade) var teams: [Team]
    var pro: Bool
    
    init(teams: [Team], pro: Bool) {
        self.teams = teams
        self.pro = pro
    }
    func toTeamArr() -&gt; [Team] {
        return teams
    }
    
}

@Model
class Team: Identifiable, Hashable {
    @Attribute(.unique) var id: UUID = UUID()
    var name: String
    var score: Int
    @Transient var editing: Bool = false
}

The .cascade argument to @Relationship tells SwiftData to delete all of the referenced Team objects if the CounterModel object is deleted.

huangapple
  • 本文由 发表于 2023年6月13日 10:28:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76461362.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定