my json file doesn't appear in my index swift

Viewed 36

Here I have a new problem: My application compiles but unfortunately, I can't open the files contained in the json file. My indexViewController file doesn't display it.

Here is the link of my json file: https://drive.google.com/file/d/1qMKLNcf1fvYla21Yhh_D36FuPI-0-kj2/view?usp=drivesdk and here is the code of my indexviewcontroller: https://drive.google.com/file/d/169H5w6WvqAE43B4VceXqvSrHJXJUzE8Y/view?usp=sharing

Please help me I am still a beginner in Swift programming

class IndexViewController: UIViewController,  UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate{
    
    @IBOutlet weak var indexTableView: UITableView!
    @IBOutlet weak var bookmarkButton: UIBarButtonItem!
    
    // The BookViewModel init method decodes the json file and sets the chapters property on this instance
    var book: BookViewModel = BookViewModel()
    var chapters: [ChapterViewModel] = [ChapterViewModel]()
    var managedObjectContext: NSManagedObjectContext!
    
    var hasUserTappedOnBookmarkWidget: Bool = false
    
    let searchController = UISearchController(searchResultsController: nil)
    var filteredChapters: [ChapterViewModel] = [ChapterViewModel]()
    var chapNumberAndSearchTextRangeList: [Int: [ChapterViewModel]] = [Int: [ChapterViewModel]]()
    var searching = false
    lazy var workItem = WorkItem()
    
    private var userClickedChapter: ChapterViewModel?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        chapters = book.chapters
        setNavBarLargeTitleProperty()
        
        // This makes the table view row animate its selection state as it pops and pushes on the navigation stack.
        if let selectedIndex = indexTableView.indexPathForSelectedRow {
            
            let coordinator = self.transitionCoordinator

            if coordinator != nil {
                coordinator?.animate(alongsideTransition: { (context) in
                    self.indexTableView.deselectRow(at: selectedIndex, animated: true)

                }, completion: { (context) in

                    if context.isCancelled {
                        self.indexTableView.selectRow(at: selectedIndex, animated: false, scrollPosition: .none)
                    } else {
                        self.indexTableView.deselectRow(at: selectedIndex, animated: true)
                    }
                })
            } else {
                self.indexTableView.deselectRow(at: selectedIndex, animated: true)
            }
        }
        
        // if the user has tapped on the widget, navigate to BookmarkViewController directly
        if hasUserTappedOnBookmarkWidget {
            performSegue(withIdentifier: "goToBookmarks", sender: self)
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Set the datasource and delegate as self
        indexTableView.dataSource = self
        indexTableView.delegate = self
        
        indexTableView.register(UINib(nibName: "IndexCell", bundle: nil), forCellReuseIdentifier: "customIndexCell")
1 Answers

The JSON you posted is an invalid JSON, try validating your JSON in some online linter to see if your JSON is in correct format. After validation and making some changes, your JSON should look something like this. (Since your JSON is too large It is uploaded on disk and you cannot check the preview directly on SO page :/ ) Afterwards check the decoder to make sure your object is decoded properly. From the JSON you posted you can decode Book object that has following structure:

struct Book {
  let name: String?
  let chapters: [Chapter]?
}

struct Chapter {
  let number: Int?
  let name, contents: String?
}
Related