Monday, November 28, 2022
HomeiOS Developmentios - Information is just not saved or load due to i'm...

ios – Information is just not saved or load due to i’m name the information from one other pages


Mannequin

I’ve completely different variables in Journey.

import Basis

struct Journey: Identifiable, Codable {
    var id = UUID()
    var title: String
    var date = Date()
    var location: String
    var isFav: Bool
    var rating: Float
    var remark: String
}

View mannequin

I load and save knowledge with UserDefaults. At all times its work however on this mannequin not.

import Basis


class TravelViewModel: ObservableObject {
    @Revealed var travelList = [Travel] ()
    @Revealed var travelled = 0
    
    init(){
        load()
    }
    
    func load() {
        guard let knowledge = UserDefaults.customary.knowledge(forKey: "travelList"),
              let savedTravels = attempt? JSONDecoder().decode([Travel].self, from: knowledge) else { travelList = []; return }
        
        travelList = savedTravels
    }
    
    func save() {
        do {
            let knowledge = attempt JSONEncoder().encode(travelList)
            UserDefaults.customary.set(knowledge, forKey: "travelList")
        } catch {
            print(error)
        }
    }
    
}

Including Merchandise View

I’ve addItems func and use this func in addItem button.

import SwiftUI

struct AddTravelView: View {
    
    @StateObject var VM = TravelViewModel()
    
    @State var title = ""
    @State var location = ""
    @State var isFav = false
    @State var rating = 0.00
    @State var remark = ""
    
    var physique: some View {
        VStack {
            ZStack {
                Rectangle()
                    .fill(.black.opacity(0.2))
                    .cornerRadius(20)
                    .body(width: 350, peak: 350)
                VStack{
                    HStack {
                        Textual content("Title:")
                            .font(.system(measurement: 16 , weight: .medium))
                        TextField("Sort..", textual content: $title)
                    }
                    HStack {
                        Textual content("Location:")
                            .font(.system(measurement: 16 , weight: .medium))
                        TextField("Sort..", textual content: $location)
                    }
                    HStack {
                        Textual content("Rating: (Int(rating))")
                            .font(.system(measurement: 16 , weight: .medium))
                        Slider(worth: $rating, in: 0...10, step: 1)
                    }
                    Spacer()
                    
                    ZStack(alignment: .topLeading) {
                        Rectangle()
                            .fill(.white)
                            .cornerRadius(20)
                            .body(peak: 200)
                        VStack(alignment: .main) {
                            TextField("Remark...", textual content: $remark, axis: .vertical)
                        }.padding()
                    }
                }
                .padding()
                .body(width: 300, peak: 200)
            }
            
            Button {
                addTravel()
            } label: {
                ZStack{
                    Rectangle()
                        .fill(.black.opacity(0.2))
                        .cornerRadius(20)
                        .body(width: 350 , peak: 100)
                        
                    
                    Textual content("ADD TRAVEL")
                        .font(.system(measurement: 25, weight: .medium, design: .monospaced))
                        .foregroundColor(.black)
                }.padding()
            }

            
        }
    }
    
    func addTravel(){
        VM.travelList.append(Journey(title: title, location: location, isFav: isFav, rating: Float(rating), remark: remark))
    }
    
}

struct AddTravelView_Previews: PreviewProvider {
    static var previews: some View {
        AddTravelView()
    }
}

Latest Provides view

On this web page i wanna see Objects i add earlier than

import SwiftUI

struct RecentTravels: View {
    
    @StateObject var VM = TravelViewModel()
    
    var physique: some View {
        VStack {
            ForEach(VM.travelList) {Journey in
                HStack{
                    Picture(systemName: "questionmark")
                        .body(width: 50, peak: 50)
                        .padding()
                        .overlay {
                            RoundedRectangle(cornerRadius: 5)
                                .stroke(.black, lineWidth: 2)
                        }
                    VStack(alignment: .main) {
                        Textual content(Journey.title)
                            .font(.subheadline)
                            .daring()
                            .lineLimit(1)
                        Textual content("(Journey.date)")
                            .font(.footnote)
                            .opacity(0.9)
                            .lineLimit(1)
                    }
                    
                    Spacer()
                    VStack {
                        Picture(systemName: "coronary heart")
                        Spacer()
                        Textual content("(Journey.rating)")
                    }
                    .body(peak: 50)
                    .font(.system(measurement: 22))
                    
                }
            }
        }
    }
}

struct RecentTravels_Previews: PreviewProvider {
    static var previews: some View {
        RecentTravels()
    }
}

And ContentView

and calling these 2 views in ContentView.

import SwiftUI

struct ContentView: View {
    
    @StateObject var VM = TravelViewModel()
    
    var physique: some View {
        VStack {
            
            AddTravelView()
            
            RecentTravels()
        }
        .padding()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

When i write the all code in solely ContentView is figure however when i name one other pages its not work. Often it was work when i presss add merchandise button and restart app however now its nothing. Its not work even restart the app.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments