ios – the best way to change the info loaded from an area json file in swift and current the modified datat concurrently in numerous pages

0
67

[ad_1]

I used to be making an attempt to parse an area JSON file into my very own struct, it work nicely. Now I wish to change some values like DOUBAN_VOTES within the array. I do not understand how to try this and the best way to present this modified information concurrently on totally different pages.

my struct

import Basis


struct Film:Codable {
    var MOVIE_ID:String
    var NAME:String
    var ALIAS:String?
    var ACTORS:String?
    var COVER:String?
    var DIRECTORS:String?
    var DOUBAN_SCORE:String?
    var DOUBAN_VOTES:String?
    var GENRES:String?
    var IMDB_ID:String?
    var LANGUAGES:String?
    var MINS:String?
    var OFFICIAL_SITE:String?
    var REGIONS:String?
    var RELEASE_DATE:String?
    var SLUG:String?
    var STORYLINE:String?
    var TAGS:String?
    var YEAR:String?
    var ACTOR_IDS:String?
    var DIRECTOR_IDS:String?
    var isFavered:Bool = false
    enum CodingKeys: String, CodingKey {
        case MOVIE_ID, NAME,ALIAS,ACTORS,COVER,DIRECTORS,DOUBAN_SCORE
        case DOUBAN_VOTES,GENRES,IMDB_ID,LANGUAGES,MINS,OFFICIAL_SITE
        case REGIONS,RELEASE_DATE,SLUG,STORYLINE,TAGS,YEAR,ACTOR_IDS,DIRECTOR_IDS
    }
    
}
class moviedata: ObservableObject {
    @Printed var myMovieData:[Movie] = Bundle.predominant.decode(filename:"movieData1.json")
}

extension Bundle{
    func decode<T:Codable>(filename:String) -> T {
        guard let url = Bundle.predominant.url(forResource: filename, withExtension: nil),
              let information = attempt? Information(contentsOf: url),
              let getResponse = attempt? JSONDecoder().decode(T.self, from: information)
              else { fatalError("no information")}
        return getResponse
    }
}

my SwiftUI file: I wish to move the worth to the array when click on button “affirm”


import SwiftUI

struct myPage: View {
    //@State var movieData: [Movie] = []
    @ObservedObject var movieData=moviedata()
    @State non-public var index = 0
    @State non-public var nf = NumberFormatter()
    @State non-public var temp:Int=0;
    var physique: some View {
        VStack {
            //Textual content(movieData.myMovieData[index].NAME)
            Textual content("vote: (temp)")
            
            //Textual content(verbatim: vote)
            Button("subsequent"){
                index=index+1
            }
            Button("add"){
                temp = temp + 1
                
            }
            Button("comfirm"){
                movieData.myMovieData[index].DOUBAN_VOTES=String(temp)
            }
            
        }.onChange(of: index, carry out: { worth in
           
                temp=nf.quantity(from: movieData.myMovieData[index].DOUBAN_VOTES ?? "0") as! Int
           
        })
        .onAppear{
            temp=nf.quantity(from: movieData.myMovieData[index].DOUBAN_VOTES ?? "0") as! Int
        }

    }
}

struct myPage_Previews: PreviewProvider {
    static var previews: some View {
        myPage()
    }
}

[ad_2]