Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to understand SwiftUI and have a simple app in mind to help me learn. My app has one model and two views, shown below.
The first question is specific: how do I get "update tournament" to work? I can't figure out which, if any, variable I should bind to or if that's even the right approach.
struct Tournament { // eventually will have more properties, such as Bool, Date, arrays, etc.
var name: String
var location: String = "Franchises"
#if DEBUG
var tournamentData = [
Tournament(name: "Season Opener"),
Tournament(name: "May Day Tournament"),
Tournament(name: "Memorial Day Tournament"),
Tournament(name: "School's Out Tournament")
]
#endif
}
struct TournamentListView: View {
// should I use a different property wrapper if this is going to be something I refer to throughout the app?
#State var tournaments = tournamentData
var body: some View {
NavigationView {
VStack {
List(tournaments, id: \.name) { tournament in
NavigationLink(
destination: TournamentDetailView(tournaments: $tournaments, tournament: tournament),
label: { Text (tournament.name)}
)
}
Spacer()
NavigationLink(
destination: TournamentDetailView(tournaments: $tournaments, addingNewTournament: true),
label: { Text ("Add a new tournament") }
)
}.navigationTitle("Tournaments")
}
}
}
struct TournamentDetailView: View {
#Environment(\.presentationMode) var presentationMode
#Binding var tournaments: [Tournament]
#State var tournament = Tournament(name: "")
var addingNewTournament: Bool = false
var body: some View {
Form {
Section(header: Text("Tournament Info")) {
TextField("Name", text: $tournament.name)
TextField("Location", text: $tournament.location)
}
Section {
Button(action: {
if addingNewTournament { tournaments.append(tournament) }
presentationMode.wrappedValue.dismiss()
}) { Text(addingNewTournament ? "Create Tournament" : "Update Tournament") }
}
}
}
}
(P. S. I removed the second question about if my code is more or less good or if I'm not doing things "The SwiftUI Way" because my question was closed ("this will help others answer the question" my left foot!). Another case of SO getting in its own way, being an obstacle to people learning rather than a vehicle for good. I asked a very specific question, but I also took the opportunity to ask if my approach was reasonable or not. This should NOT be something that admins police, especially since, the one reply I did get actually answered a question I DID NOT ASK, but had thought about, namely about Previews. So, let me get this straight: I can only ask a single question or I'm sanctioned, but people can answer more than was asked; they just need to be clairvoyant. That's nuts. I think the reply I got exemplifies the good parts of SO - people helping people and even going the extra mile. My second question of an obviously much more general nature was not going to confuse anyone who read my question.)
First thing is, you should keep in mind that you have just one source of truth to drive your entire application. This source of truth will be passed around as an EnvironmentObject. So all your tournamentData will be inside this class. For example:
final class DataStore : ObservableObject{
#if DEBUG
#Published var tournamentData = [
Tournament(name: "Season Opener"),
Tournament(name: "May Day Tournament"),
Tournament(name: "Memorial Day Tournament"),
Tournament(name: "School's Out Tournament")
]
#else
#Published var tournamentData = [Tournament]()
#endif
}
This should be passed around from your top level view.
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(DataStore())
}
}
And just access this from each of your child views.
#EnvironmentObject var store : DataStore
Then you should just update this single source of truth and the tournamentData inside of it. Everything should work as expected.
I recommend seeing apple's WWDC SwiftUI videos. They explain it pretty well. Or if you want a more hands on approach first, go through this tutorial. It is amazing.
Edit: For completion sake, I think I should add in the proper method of handling DEBUG only code as well. The above will work but what you should really do is assign models for debugging inside the PreviewProvider. Apple removes it from production code anyways.
So inside your View:
struct Listing_Previews: PreviewProvider {
static var previews: some View {
let store = DataStore()
store.tournamentData = [
Tournament(name: "Season Opener"),
Tournament(name: "May Day Tournament"),
Tournament(name: "Memorial Day Tournament"),
Tournament(name: "School's Out Tournament")
]
return Listing().environmentObject(store)
}
}
and remove the #if DEBUG from your DataStore
Related
I want to link to a view that contains a non-view object - created once per user tap of the "Start" link - that is dependent on data selected by the user. The code below is as close as I've gotten. QuestionView.init is called as soon as HomeView appears, and again every time you select a new value for Highest Number, thus creating the Question object repeatedly, which is what I'm trying to avoid. I want to only create the Question object one time - when the user taps on the "Start" link.
I've tried many different approaches. It feels like I am stuck problem solving from an imperative UI oriented approach, instead of the new-for-me declarative approach of SwiftUI. Perhaps there's a bridge I'm missing from the state-driven approach of Views to the more familiar-to-me non-view objects, like my Question object I want to create only once.
struct Question {
let value1: Int
let value2: Int
let answer: Int
init(_ highestNumber: Int) {
print("Question.init \(highestNumber)")
value1 = Int.random(in: 1...highestNumber)
value2 = Int.random(in: 1...highestNumber)
answer = value1 * value2
}
var prompt: String {
"\(value1) x \(value2) = ?"
}
}
struct HomeView: View {
#State var highestNumber: Int = 12
var body: some View {
NavigationStack {
Picker("Highest Number", selection: $highestNumber) {
ForEach(4...12, id: \.self) { Text(String($0)) }
}
.pickerStyle(.wheel)
NavigationLink(destination: QuestionView(highestNumber: $highestNumber)) {
Text("Start")
}
}
}
}
struct QuestionView: View {
#Binding var highestNumber: Int
#State var question: Question
init(highestNumber: Binding<Int>) {
print("QuestionView.init")
self._highestNumber = highestNumber
question = Question(highestNumber.wrappedValue)
}
var body: some View {
Text(question.prompt)
Button("I got it") {
question = Question(highestNumber)
}
}
}
I am trying to create a LazyVGrid based on user selection in another view. As follows, the peoplelist and selectedpersonID are coming from other view.
I understand that I cannot use the "selectedpersons" during initializing of this view. I looked here(Cannot use instance member 'service' within property initializer; property initializers run before 'self' is available) to use onAppear() of the LazyVGrid.
It goes well during compiling and works ok if you select 1 person.
Once I selected 2 persons, I got a Fatal error that Index out of range at row.
struct Someview: View {
#ObservedObject var peoplelist : PersonList
let selectedpersonID : Set<UUID>?
#State private var days : [String] = Array(repeating: "0", count: selectedpersons.count * 5) //got first error here, during compiling
var body: some View {
VStack{
LazyVGrid(columns: columns) {
Text("")
ForEach(1..<6) { i in
Text("\(i)").bold()
}
ForEach(0..< selectedpersons.count , id: \.self) { row in
Text(selectedpersons[row].Name)
ForEach(0..<5) { col in
TextField("", text: $days[row * 5 + col])
}
}
}
.onAppear(){
days = Array(repeating: "0", count: selectedpersons.count * 5)}//no problem during compiling, but will have error when more than 1 person are selected.
.padding()
}
}
var selectedpersons: [Persons] {
return peoplelist.persons.filter {selectedpersonID!.contains($0.id)}
}
}
It seems to me that this OnAppear() is still slower than the content inside the LazyVGrid? So, the days is not changed quick enough for building the content insider the LazyVGrid?
Or did I make an error of the index in the array of days?
It's crashing because ForEach isn't a for loop its a View that needs to be supplied Identifiable data. If you're using indices, id: \self or data[index] then something has gone wrong. There are examples of how to use it correctly in the documentation.
Also onAppear is for performing a side-effect action when the UIView that SwiftUI manages appears, it isn't the correct place to set view data, the data should be already in the correct place when the View struct is init. Making custom Views is a good way to solve this.
I have an app that lists Events from Core Data. Each event has a date.
When I list the Events, I want show the date, unless the date was today or yesterday, and in that case I want to show Today or Yesterday instead of the date.
As of now, I have a function that handles generating the String to show in the row. However, I've noticed that if a day passes and I re-open the app, it shows outdated information. For example, if there is an event from the previous day that said Today when I had the app open the previous day, it will still say Today instead of Yesterday when I re-open the app. Obviously this function is not being called every time I open the app, but I am wondering what the best approach is for making this more dynamic.
These are the avenues I am considering, but not sure what would be best, so I wanted to post here to get recommendations and see if I'm overlooking anything important:
Somehow do something with .onAppear on the row to re-calculate it every time the app is opened (I'm not sure how expensive this date calculation stuff is for each event, but even if it's not expensive I'm not sure how I would tell the rows to re-run the function when the app comes to the foreground)
Switch to a computed property (I don't know if this would be any different than putting a function in there, like I have now. This could be bad to have it called every time if it's an expensive call, but assuming it's not how would I get this to refresh every time the app comes to the foreground?)
Come up with a solution to only re-calculate each row if the day has changed (this is probably what I'd try to do if I knew the row calculation was very expensive, but seems like it might be overkill here, and I'm also not sure how I would go about telling each row to re-run the function)
Here is my code (I left out my date formatter code, but it's pretty standard and shouldn't matter for this):
struct ContentView: View {
#FetchRequest(fetchRequest: Event.eventsNewestFirst)
private var events: FetchedResults<Event>
var body: some View {
NavigationView {
ForEach(events){ event in
EventRow(event: event)
}
}
}
}
struct EventRow: View {
#ObservedObject var event: Event
var body: some View {
Text(event.dateAndTimeString())
}
}
extension Event {
func dateAndTimeString() -> String {
guard let date = self.date else { return "Error" }
let timeString = DateAndNumberFormatters.simpleTimeDisplay.string(from: date)
let dateString: String
if let todayOrYesterday = date.asTodayOrYesterday() {
dateString = todayOrYesterday
} else {
dateString = DateAndNumberFormatters.simpleShortDateDisplay.string(from: date)
}
return "\(dateString) at \(timeString)"
}
}
extension Date {
func asTodayOrYesterday() -> String? {
let calendar = Calendar.current
let dayComponents = calendar.dateComponents([.year, .month, .day], from: self)
let todayDateComponents = calendar.dateComponents([.year, .month, .day], from: Date())
var yesterdayDateComponents = calendar.dateComponents([.year, .month, .day], from: Date())
yesterdayDateComponents.day = yesterdayDateComponents.day! - 1
let dayDate: Date! = calendar.date(from: dayComponents)
let todayDayDate: Date! = calendar.date(from: todayDateComponents)
let yesterdayDayDate: Date! = calendar.date(from: yesterdayDateComponents)
switch dayDate {
case todayDayDate:
return "Today"
case yesterdayDayDate:
return "Yesterday"
default:
return nil
}
}
}
The possible approach is to observe scene phase and force refresh observed core data object as needed, like
struct EventRow: View {
#ObservedObject var event: Event
#Environment(\.scenePhase) var scenePhase
var body: some View {
Text(event.dateAndTimeString())
.onChange(of: scenePhase) {
if $0 == .active {
event.objectWillChange.send()
}
}
}
}
The scenePhase approach in another answer did not work.
The solution I ended up using relies on a publisher of UIApplication.didBecomeActiveNotification instead:
struct EventRow: View {
#ObservedObject var event: Event
#State private var dateAndTime: String = "Error"
var body: some View {
NavigationLink(destination: EventDetailView(event: event)) {
Text(dateAndTime)
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
dateAndTime = event.dateAndTimeString()
}
}
}
}
So I'm retrieving data from FireStore. I'm retrieving the data successfully. When I tap my search button the first time the data is being downloaded and the new view is pushed. As a result, I get a blank view. But when I go back, hit search again, sure enough I can see my data being presented.
How can I make sure I first have the data I'm searching for THEN navigate to the new view? I've used #State variables etc. But nothing seems to be working. I am using the MVVM approach.
My ViewModel:
class SearchPostsViewModel: ObservableObject {
var post: [PostModel] = []
#State var searchCompleted: Bool = false
func searchPosts(completed: #escaping() -> Void, onError: #escaping(_ errorMessage: String) -> Void) {
isLoading = true
API.Post.searchHousesForSale(propertyStatus: propertyStatus, propertyType: propertyType, location: location, noOfBathrooms: noOfBathroomsValue, noOfBedrooms: noOfBedroomsValue, price: Int(price!)) { (post) in
self.post = post
print(self.post.count)
self.isLoading = false
self.searchCompleted.toggle()
}
}
}
The code that does work, but with the bug:
NavigationLink(destination: FilterSearchResults(searchViewModel: self.searchPostsViewModel)
.onAppear(perform: {
DispatchQueue.main.async {
self.createUserRequest()
}
})
)
{
Text("Search").modifier(UploadButtonModifier())
}
Try with the following modified view model
class SearchPostsViewModel: ObservableObject {
#Published var post: [PostModel] = [] // << make both published
#Published var searchCompleted: Bool = false
func searchPosts(completed: #escaping() -> Void, onError: #escaping(_ errorMessage: String) -> Void) {
isLoading = true
API.Post.searchHousesForSale(propertyStatus: propertyStatus, propertyType: propertyType, location: location, noOfBathrooms: noOfBathroomsValue, noOfBedrooms: noOfBedroomsValue, price: Int(price!)) { (post) in
DispatchQueue.main.async {
self.post = post // << update on main queue
print(self.post.count)
self.isLoading = false
self.searchCompleted.toggle()
}
}
}
}
You should look at the Apple documentation for #State and ObservableObject
https://developer.apple.com/documentation/combine/observableobject
https://developer.apple.com/documentation/swiftui/state
Your issue is with using an #State in a non-UI class/View.
It might help if you start with the Apple SwiftUI tutorials. So you understand the differences in with the wrappers and learn how it all connects.
https://developer.apple.com/tutorials/swiftui
Also, when you post questions make sure your code can be copied and pasted onto Xcode as-is so people can test it. You will get better feedback if other developers can see what is actually happening. As you progress it won't be as easy to see issues.
I'm working through this SwiftUI tutorial here:
https://developer.apple.com/tutorials/swiftui/handling-user-input
And on Step 3 of the section "Adopt the Model Object in Your View", I get this error on the Toggle statement in line 16: "Generic parameter 'Label' could not be inferred."
My code is identical to that provided in the tutorial:
import SwiftUI
struct LandmarkList: View {
#EnvironmentObject var userData: UserData
var body: some View {
NavigationView {
List {
Toggle(isOn: $userData.showFavoritesOnly) {
Text("Favorites Only")
}
ForEach(userData.landmarkData) { landmark in
if !self.userData.showFavoritesOnly || landmark.isFavorite {
NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
}
}
.navigationBarTitle(Text("Landmarks"))
}
}
}
struct LandmarkList_Previews: PreviewProvider {
static var previews: some View {
ForEach(["iPhone SE", "iPhone XS Max"], id: \.self) { deviceName in
LandmarkList()
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}
}
}
When I look at the code provided in the "Complete" folder, I see its nearly identical, except that the userData variable is made private—which I added to my "StartingPoint" version, though I can't imagine why it'd make a difference, and of course it still gives the same error and won't build. I can build and run the Complete version, so clearly the message about requiring a Generic parameter is wrong and it must have to do with something else like how the project is configured in settings.
I remember getting stuck earlier this summer with a similar issue in a different part of the tutorial, and found a post where someone explained why code would work in one project and not another, but I can't find that post now.
Is anyone familiar with this issue? Is there something else I need to understand about how to configure my project before I can reference an observable object in a toggle control in my view like this?
So as I mentioned in the comments: Just add .environmentObject(UserData()) under the ForEach() in your LandmarkList_Previews struct.
That would result in:
import SwiftUI
struct LandmarkList: View {
#EnvironmentObject var userData: UserData
var body: some View {
NavigationView {
List {
Toggle(isOn: $userData.showFavoritesOnly) {
Text("Favorites Only")
}
ForEach(userData.landmarkData) { landmark in
if !self.userData.showFavoritesOnly || landmark.isFavorite {
NavigationLink(destination: LandmarkDetail(landmark: landmark)
.environmentObject(self.userData)
) {
LandmarkRow(landmark: landmark)
}
}
}
}
.navigationBarTitle(Text("Landmarks"))
}
}
}
struct LandmarkList_Previews: PreviewProvider {
static var previews: some View {
ForEach(["iPhone SE", "iPhone XS Max"], id: \.self) { deviceName in
LandmarkList()
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}.environmentObject(UserData())
}
}
I had the exact same problem today and Ravi Mishra's comment resolved the issue for me (kudos to you). It must've been an autocomplete mistake.
ForEach(userData.landmarkData) should instead be ForEach(userData.landmarks)
I bothered my head about this tutorial for a week, went line by line through it twice by downloading the tutorial files again. I made sure that every character of my work in the tutorial matched every character of the completed project--twice--and still that nasty error message popped up as soon as I entered the final code. Stack Overflow was my last stop; when I read the solution was to include '.environmentObject(UserData())', I went right to my code to add it but it was already there!
Out of pure desperation, I decided to copy the (exact same) code for the body from the source of the completed project to the source in my tutorial project. The error disappeared.
I think #krjw has a point.