每周活动总结有帮助吗?斯维夫图伊
我正在尝试获取必须每周设置的活动列表(“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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在
ForEach
和ActivityRow
中调用obj.activity
,这就是它在各处重复相同静态属性的原因。你最好删除你的 struct obj 并在没有它的情况下重试
You're calling
obj.activity
in yourForEach
andActivityRow
, 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在您的 Persistent 对象中,您有一个由许多活动组成的数组,称为 Activitylist,但只有一个布尔值可以告诉您是否实现了目标 - 实际上是 goalachieved 。
您的视图正在遍历
Persistent.activitylist
数组,因此对于一个结果(已实现或未实现),您将有很多行。您实际上可能想要迭代Persistent
对象的数组 - 这意味着您可能应该将[Persistent]
存储在某个变量中。这样,您将只看到每个结果的一行。如果我也可能建议:使用命名变量的约定,Swift使用“camelCaseConventionForVariables”,比“thewholevariableislowercase”更容易阅读
编辑:
让我尝试稍微改变一下你的代码(我个人会改变它更激进,但这不是答案的范围)。
不要让数组
activityList
上的所有元素只有一个goalAchieved
,而是将其设为字典:将值添加到字典中(@meomeomeo 是对的,您不需要 < code>obj):
迭代 ForEach 中的字典;有关更多信息:阅读此处。
In your
Persistent
object you have an array of many activities, calledactivitylist
, 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 ofPersistent
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 arrayactivityList
, make it a dictionary:Add values to the dictionary (@meomeomeo is right, you don't need
obj
):Iterate on the dictionary in your ForEach; for more info: read here.