Newest version of Swift Playgrounds for iPad (3.4) fails to read imported JSON file.

In the previous version of the Swift Playgrounds for iPad app, I was able to rely on using data through JSON files with companion struct.

This code is cut way back to demonstrate that JSON data is not processed. It never fails, but then it also never does anything either.

This code was also tested (for any syntax errors) on Xcode 12.2 on MBP running Catalina 10.15.7 where it ran flawlessly. Before the previous Swift Playgrounds update, it ran flawlessly on iPad as well.

Code Block swift
import PlaygroundSupport
import SwiftUI
struct MovieObject: Codable, Identifiable {
    let id: Int
    let thumbnail: String
    let title: String
    let description: String
    let trailerLink: String
    let catagory: String
    let isFeaturedMovie: Bool
}
let movies = Bundle.main.decode([MovieObject].self, from: "movie_data.json")
struct ContentView: View {
    var body: some View {
        ForEach(movies) { movie in 
            Text("\(movie.title)")
        }
    }
}
import UIKit
extension Bundle {
    func decode<T: Decodable>(_ type: T.Type, from file: String) -> T {
        guard let url = self.url(forResource: file, withExtension: nil) else {
            fatalError("Failed to locate \(file) in bundle.")
        }        
        guard let data = try? Data(contentsOf: url) else {
            fatalError("Failed to load \(file) from bundle.")
        }
        let decoder = JSONDecoder()
        guard let loaded = try? decoder.decode(T.self, from: data) else {
            fatalError("Failed to decode \(file) from bundle.")
        }
        return loaded
    }
}
PlaygroundPage.current.setLiveView( ContentView() )



A sample of the JSON file called "movie_data.json" that was imported to the Playground with the + button is seen here:

Code Block json
[
{
"id": 1,
"thumbnail": "place some url here this forum doesn't allow links",
"title": "Aquaman",
"description": "Once home to the most ...",
"trailerLink" : "place some url here this forum doesn't allow links",
"catagory": "DC",
"isFeaturedMovie": true
},
{
"id": 2,
"thumbnail": "place some url here this forum doesn't allow links",
"title": "The Avengers",
"description": "S.H.I.E.L.D. leader Nick Fury is blah blah blah ",
"trailerLink" : "place some url here this forum doesn't allow links",
"catagory": "Featured",
"isFeaturedMovie": false
},
{
"id": 3,
"thumbnail": "place some url here this forum doesn't allow links",
"title": "Avengers - Age Of Ultron",
"description": "Tony Stark builds ... blah blah blah",
"trailerLink" :"place some url here this forum doesn't allow links",
"catagory": "Marvel",
"isFeaturedMovie": false
},
]


The full blown app (before I trimmed it down severally) had scrollable thumbnails of various movies in horizontal rows in a sidebar ... when one was tapped, a detailed view gave a description with a Play button that went to You Tube to play the trailer in the browser ... it ran flawlessly on iPad Swift Playgrounds app.

I have probably placed enough post to demonstrate that I think the newest version of this app is pretty much busted. Looking forward to some major damage control in a future update to fix things back up.

Replies

Did you file a bug report through Feedback Assistant? Also, if you eliminate the SwiftUI (still need to import UIKit for Bundle) and just invoke something like

Code Block
movies.forEach { print(“\($0.title)”) }


does it behave as expected? (I just did this with your code, and there doesn’t seem to be a problem with the decoding. I have to say, having the console, added in this release, is quite nice for debug logging.)