每周活动总结有帮助吗?斯维夫图伊

发布于 2025-01-11 12:44:09 字数 2661 浏览 1 评论 0原文

我正在尝试获取必须每周设置的活动列表(“dd/mm/YY:实现的目标/错过的目标”)。问题是我获得了与前一项具有相同日期和相同结果的活动列表。例如:

28/02/2022: goal achieved
28/02/2022: goal achieved
28/02/2022: goal achieved

和第二天:

01/03/2022: missed goal
01/03/2022: missed goal
01/03/2022: missed goal
01/03/2022: missed goal

我想获得一个列表,例如:

28/02/2022: goal achieved
01/03/2022: missed goal
02/03/2022: goal achieved...

这些是有用的结构:

struct Persistent {
 @AppStorage("goalAchieved") static var goalAchieved : Bool = false
 @AppStorage("activityList") static var activityList : [String] = []
}

struct obj {
static var currentDate = Date()
static var stringDate = ""
static var activity = Activity(date:Persistent.lastUpdatedDate)
}

这是 ActivityListView:

import SwiftUI

func activitystring(activity:Activity) -> String{
var output = ""
output = "\(activity.date): \(activity.reachedobj(goalAchieved: Persistent.goalAchieved))"
return output

}

struct Activity: Identifiable{

let id = UUID()
let date: String

func reachedobj(goalAchieved: Bool) -> String {
     var output = ""
    if Persistent.goalAchieved == false { output = "Missed goal" }
    if Persistent.goalAchieved == true { output = "Goal Achieved!"}
     
     return output
     
 }

 }

struct ActivityRow: View{

var activity: Activity


var body: some View{
    Text(activitystring(activity: activity))
    Divider()
}
}



struct ActivityListView: View {


var body: some View {
    
    ScrollView{
        
        Text("Week summary").font(.system(size: 15)).foregroundColor(Color.green)
        
        Text("")
        
    ForEach(Persistent.activityList, id: \.self) { activity in 
        
        let activity = Activity(date: Persistent.lastUpdatedDate)
        ActivityRow(activity: activity)
        
        }
    
    }
}

}

最后,这是 ApplicationApp 文件(主)中的有用代码,其中我更新活动列表:

MenuView().onAppear(){
                    
       if Persistent.activityList.count>7{
          Persistent.activityList.removeAll()
       }
                    
      obj.currentDate = Date()
      let formatter = DateFormatter()
      formatter.dateFormat = "dd/MM/YY"
      obj.stringDate = formatter.string(from:obj.currentDate)

      if Persistent.lastUpdatedDate != obj.stringDate{

      Persistent.goalAchieved = false
      let activity = Activity(date: Persistent.lastUpdatedDate)
      Persistent.activityList.append(activitystring(activity: activity))
      Persistent.lastUpdatedDate = obj.stringDate

      }                  
 }
    

这有什么问题吗?

I'm tryin' to obtain a list of activities ("dd/mm/YY: goal achieved/missed goal") which has to be setted every week. The problem is that I obtain a list of activities with the same date and the same result of the previous one. For example:

28/02/2022: goal achieved
28/02/2022: goal achieved
28/02/2022: goal achieved

and the next day:

01/03/2022: missed goal
01/03/2022: missed goal
01/03/2022: missed goal
01/03/2022: missed goal

I want to obtain, instead, a list like:

28/02/2022: goal achieved
01/03/2022: missed goal
02/03/2022: goal achieved...

These are useful structs:

struct Persistent {
 @AppStorage("goalAchieved") static var goalAchieved : Bool = false
 @AppStorage("activityList") static var activityList : [String] = []
}

struct obj {
static var currentDate = Date()
static var stringDate = ""
static var activity = Activity(date:Persistent.lastUpdatedDate)
}

This is the ActivityListView:

import SwiftUI

func activitystring(activity:Activity) -> String{
var output = ""
output = "\(activity.date): \(activity.reachedobj(goalAchieved: Persistent.goalAchieved))"
return output

}

struct Activity: Identifiable{

let id = UUID()
let date: String

func reachedobj(goalAchieved: Bool) -> String {
     var output = ""
    if Persistent.goalAchieved == false { output = "Missed goal" }
    if Persistent.goalAchieved == true { output = "Goal Achieved!"}
     
     return output
     
 }

 }

struct ActivityRow: View{

var activity: Activity


var body: some View{
    Text(activitystring(activity: activity))
    Divider()
}
}



struct ActivityListView: View {


var body: some View {
    
    ScrollView{
        
        Text("Week summary").font(.system(size: 15)).foregroundColor(Color.green)
        
        Text("")
        
    ForEach(Persistent.activityList, id: \.self) { activity in 
        
        let activity = Activity(date: Persistent.lastUpdatedDate)
        ActivityRow(activity: activity)
        
        }
    
    }
}

}

Finally this is the useful code in the ApplicationApp file (main) where I update activity list:

MenuView().onAppear(){
                    
       if Persistent.activityList.count>7{
          Persistent.activityList.removeAll()
       }
                    
      obj.currentDate = Date()
      let formatter = DateFormatter()
      formatter.dateFormat = "dd/MM/YY"
      obj.stringDate = formatter.string(from:obj.currentDate)

      if Persistent.lastUpdatedDate != obj.stringDate{

      Persistent.goalAchieved = false
      let activity = Activity(date: Persistent.lastUpdatedDate)
      Persistent.activityList.append(activitystring(activity: activity))
      Persistent.lastUpdatedDate = obj.stringDate

      }                  
 }
    

What's wrong on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦亿 2025-01-18 12:44:09

您在 ForEachActivityRow 中调用 obj.activity,这就是它在各处重复相同静态属性的原因。

你最好删除你的 struct obj 并在没有它的情况下重试

You're calling obj.activity in your ForEach and ActivityRow, that's why it repeats that same static property all over the place.

You better just drop your struct obj and try again without it

荆棘i 2025-01-18 12:44:09

在您的 Persistent 对象中,您有一个由许多活动组成的数组,称为 Activitylist,但只有一个布尔值可以告诉您是否实现了目标 - 实际上是 goalachieved 。

您的视图正在遍历 Persistent.activitylist 数组,因此对于一个结果(已实现或未实现),您将有很多行。您实际上可能想要迭代 Persistent 对象的数组 - 这意味着您可能应该将 [Persistent] 存储在某个变量中。这样,您将只看到每个结果的一行。

如果我也可能建议:使用命名变量的约定,Swift使用“camelCaseConventionForVariables”,比“thewholevariableislowercase”更容易阅读

编辑:

让我尝试稍微改变一下你的代码(我个人会改变它更激进,但这不是答案的范围)。

不要让数组 activityList 上的所有元素只有一个 goalAchieved,而是将其设为字典:

struct Persistent {

 // Drop this variable
 // @AppStorage("goalAchieved") static var goalAchieved : Bool = false

 // Make this a dictionary, the date will be the key and the goalAchieved will be the value
 @AppStorage("activityList") static var activityList : [String: Bool] = [:]
}

将值添加到字典中(@meomeomeo 是对的,您不需要 < code>obj):

MenuView().onAppear() {
                    
       if Persistent.activityList.count > 7 {
          Persistent.activityList.removeAll()
       }
                    
      let currentDate = Date()
      let formatter = DateFormatter()
      formatter.dateFormat = "dd/MM/YY"
      let stringDate = formatter.string(from: currentDate)

      if Persistent.lastUpdatedDate != stringDate {

      
          let activity = Activity(date: Persistent.lastUpdatedDate)
          Persistent.activityList[activitystring(activity: activity))] = false   // Will create something like ["01/03/2022": false]
          Persistent.lastUpdatedDate = stringDate

      }                  
 }

迭代 ForEach 中的字典;有关更多信息:阅读此处

In your Persistent object you have an array of many activities, called activitylist, but one single boolean that tells if the goal is achieved - goalachieved indeed.

Your view is iterating through the array of Persistent.activitylist, so you will have many lines for one single result - achieved or not achieved. You might actually want to iterate over an array of Persistent objects - meaning that somewhere you should probably store [Persistent] in some variable. In this way, you will see one line only for each result.

If I also may suggest: use the conventions for naming variables, Swift uses "camelCaseConventionForVariables", easier to read than "thewholevariableislowercase"

Edit:

Let me try to change a little bit your code (I would personally change it more radically, but that's not the scope of the answer).

Instead of having only one goalAchieved for all elements on the array activityList, make it a dictionary:

struct Persistent {

 // Drop this variable
 // @AppStorage("goalAchieved") static var goalAchieved : Bool = false

 // Make this a dictionary, the date will be the key and the goalAchieved will be the value
 @AppStorage("activityList") static var activityList : [String: Bool] = [:]
}

Add values to the dictionary (@meomeomeo is right, you don't need obj):

MenuView().onAppear() {
                    
       if Persistent.activityList.count > 7 {
          Persistent.activityList.removeAll()
       }
                    
      let currentDate = Date()
      let formatter = DateFormatter()
      formatter.dateFormat = "dd/MM/YY"
      let stringDate = formatter.string(from: currentDate)

      if Persistent.lastUpdatedDate != stringDate {

      
          let activity = Activity(date: Persistent.lastUpdatedDate)
          Persistent.activityList[activitystring(activity: activity))] = false   // Will create something like ["01/03/2022": false]
          Persistent.lastUpdatedDate = stringDate

      }                  
 }

Iterate on the dictionary in your ForEach; for more info: read here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文