我正在尝试在我的视图模型中递增和整数变量,它是一个 var 而不是一个 let,而且递增它的函数是结构中的一个变异函数,但我在我使用的视图中不断收到该错误消息函数。
import Foundation
import SwiftUI
struct Activity: Identifiable, Codable, Equatable, Hashable {
var id = UUID()
let title: String
let description: String
let date: Date
var completedCounts: Int
var createdDate: String {
return String(date.formatted(date: .abbreviated, time: .shortened))
}
func displayCounts(counts: Int) -> String {
if counts > 1 {
return "\(counts) times"
} else {
return "\(counts) time"
}
}
mutating func increaseCount() {
self.completedCounts += 1
}
}
class Project: ObservableObject {
@Published var activities: [Activity]
************ 我添加模型的视图如下所示 *************
Button("Save") {
if completed == true {
completedCounts += 1
}
let newActivity = Activity(title: title, description: description, date: date, completedCounts: completedCounts)
if title.count > 2 && description.count > 2 {
project.addActivity(activity: newActivity)
dismiss()
*************** 我想使用模型的视图 *********************
if completed == true {
activities.increaseCount()