I am attempting to parse XML using an URLSession, XCode 12, SwiftUI but it keeps returning [] or nil. If I print immediately after the parse(see code), all the data is there, but for some reason, it seems to be clearing it all out.
If I try it with a .xml file, the code works fine, so it must be something in my URLSession in a XMLParserDelegate class:
And then I call that with a button in my View:
If I try it with a .xml file, the code works fine, so it must be something in my URLSession in a XMLParserDelegate class:
Code Block class ParseController: NSObject, XMLParserDelegate{ var items: [Item] = [] var itemStore: [Item]? func loadData() { let url = URL(string: "website")! let request=URLRequest(url: url) let task = URLSession.shared.dataTask(with: request) { (data, response, error) in if data == nil { print("dataTaskWithRequest error: \(String(describing: error?.localizedDescription))") return } let parser = XMLParser(data: data!) parser.delegate=self parser.parse() self.itemStore=self.items print(self.itemStore) } task.resume() /* if let path = Bundle.main.url(forResource: "Items", withExtension: "xml") { if let parser = XMLParser(contentsOf: path) { parser.delegate = self parser.parse() self.itemStore=self.items } } */ }
And then I call that with a button in my View:
Code Block struct MyParserView: View { @State var itemsResult: [Item]? var body: some View { if ((itemsResult?.isEmpty) == nil) { VStack { Text("Stuff here") Button(action: { let parserControl = ParseController() parserControl.loadData() itemsResult = parserControl.itemStore }){ Text("Push") } } }else { List{ ForEach(itemResult!, id:\.id){item in Text(item.name) } } } } }