Post

Replies

Boosts

Views

Activity

Refresh App When it Enters Foreground.
Hello, I'm trying to build an app that has a message for the user. The message is time based, and therefore shows either morning, afternoon, or evening based on the time of day. However, the app does not automatically refresh when I open it, meaning that the "time of day" provided is not always accurate. I would like to use two State vars to store the time of day and pass my message into one, therefore allowing the variable to be refreshed, but I cannot find a way to do this without putting the state variables and function in the same part of the Struct, but I cannot use both at the same time. I have attached my code below, and I thank you for your help. // //  TodayView.swift import SwiftUI import Foundation import UIKit struct TodayView: View {     @Environment(.scenePhase) var scenePhase               //@Environment(.scenePhase) var scenePhase     //@State var message = Message()                //()                 @State var timeOfDay = TimeOfDay()       func Message() -> String{                                    switch month{         case 2:             switch day{             case 19:                 return "Good (timeOfDay), User."             case 20:                 return "Good (timeOfDay), User."             case 21:                 return "Good (timeOfDay),User"             case 22:                 return "Good (timeOfDay), User."             default:                 return "Houston, we have an error. Please update the app to continue using it."             }              default:       return "Please update app"     }                      }                            @State var mes = Text(Message())          var body: some View {                           NavigationView{                                           ScrollView(.vertical, showsIndicators: false){                     VStack {                         Text(Message())                             .font(Font.custom(AppFont, size: 50))                     }                                          .onChange(of: scenePhase) { newPhase in                                     if newPhase == .active {                                       timeOfDay = TimeOfDay()                                     } else if newPhase == .inactive {                                         print("Inactive")                                     } else if newPhase == .background {                                         print("Background")                                     }                                 }                }                 .navigationTitle("Daily Message")                                 // .frame(maxHeight: infinity)                 //.offset(CGSize(width: 0, height: -250))             Spacer()                             }                      }     } struct TodayView_Previews: PreviewProvider {     static var previews: some View {         TodayView()     } } I also have this: mport Foundation import SwiftUI let tghisDay = Calendar.current.dateComponents([.day, .month], from: Date()) var day = tghisDay.day! var month = tghisDay.month! var hour = tghisDay.hour! struct RealMessage{     var message:String } func TimeOfDay() -> String{                   if 11 > hour{         return "morning"     } else if 19 > hour{         return "afternoon"     } else {         return "evening"     }                            }
1
0
1.3k
Feb ’22
String as Member Name in Swift
Hello. I have an array of strings and a CoreData object with a bunch of variables stored in it; the strings represent each stored variable. I want to show the value of each of the variables in a list. However, I cannot find a way to fetch all variables from a coredata object, and so instead I'm trying to use the following code. ListView: View{ //I call this view from another one and pass in the object. let object: Object //I have a bunch of strings for each variable, this is just a few of them let strings = ["first_name", "_last_name", "middle_initial" ...] var body: some View{ List{ ForEach(strings){ str in //Want to pass in string here as property name object.str //This doesn't work because string cannot be directly passed in as property name - this is the essence of my question. } } } } So as you can see, I just want to pass in the string name as a member name for the CoreData object. When I try the code above, I get the following errors: Value of type 'Object' has no member 'name' and Expected member name following '.'. Please tell me how to pass in the string as a property name.
0
0
589
Mar ’22