Scott iOS journal

Module 5: Lesson 4

Going thru the quiz app.

Getting error

import Foundation

protocol QuizProtocol {
func questionsRetrieved(_ questions: [Question])

}

class QuizModel {

var delegate:QuizProtocol?


func getQuestions(){
    
    // TODO: Fetch the questions
    getLocalJsonFile()
    
    
}

func getLocalJsonFile (){
    // get bundle path to the json file
    let path = Bundle.main.path(forResource: "QuestionData", ofType: "json")
    
    // Double check that the path isn't nil
    guard path != nil else {
        print("Couldn't find the Json Data file")
        return
    }
    
    // create URL object from the path
    let url = URL(fileURLWithPath: path!)
    
    do {
        // Get the data from the url
        let data = try Data(contentsOf: url)
        // try to decode data into objects
        let decoder = JSONDecoder()
        
        let array = try decoder.decode([Question].self, from: data)
        
        // notify the delegate of the parsed objects
        delegate?.questionsRetrieved(array)
        
    }
    catch {
        // Error: Couldn't download the data at that url
    }
}

func getRemoteJsonFile(){
    
}

}

Code line 47
at the let array = try decoder.decode([Question].self, from data

I get error.

Class JSONDecoder requires that Question conform to Decodeable.

Searching for typos.

Note to self

delegate?.questionsRetrieved(array) are not the right colours. In the video the colours of some of the words are different.

I think you just need to go to your Question file and make it conform to Decodable

Class Question: Decodable {

Yep. Skipped over adding codable to this line in the Question.swift

What I had.

struct Question: {

What I needed

struct Question: Codable {

I skipped over two seconds of video. Cost - 2 hours of hunting for typos. But this was not a typo error this was missing a word error.

I went thru the video again and compared my code line by line.

Project compiles but the delegate?.questionsRetrieved(Array) still the wrong color.

Screen Shot 2021-03-23 at 6.09.07 PM

Does not affect the compile process but makes me wondering where I went wrong.

po data works as expected.

po array works as expected.

po questions does not work. The autocorrect(?) does not give a drop down box with a questions clickable variable.

Searching the code.

M5 L5 - quiz app

error message

Searching for typo / syntax missing words

M5 L6 - app does not display code. No compile errors. Searching code.

Sorry app does not display questions.

Got the questions to display but now the answers are missing. Just says label in the three slots at the bottom. No compiler errors. Still one formatting error - fixed and leading and trailing edges may cause clipping. Searched thru the video where Chris describes this but have not found way to fix this. Deleted all constraints on the label. This brings up other problems.

Searching func tableView for the problem of missing answers.

Can’t find my mistake.

The question displays but not the answer.

Checked the code for the display of the answers. Seems fine.

// MARK: - UITableView Delegate Methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    
    // check for nil in questions
    guard questions.count > 0 else {
        return 0
    }
    
    
    // returns the number of answers for this question
    let currentQuestion = questions[currentQuestionIndex]
    
    if currentQuestion.answers != nil {
        return currentQuestion.answers!.count
    }
    else {
        return 0
    }
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    
    // get a cell
    
    let cell = tableView.dequeueReusableCell(withIdentifier: "AnswerCell", for: indexPath)
    
    // customize it
    let label = cell.viewWithTag(1) as? UILabel
    
    if label != nil {
        
        let question = questions[currentQuestionIndex]
        
        if question.answers != nil && indexPath.row < question.answers!.count {
            // set the answer text
            // label!.text = question.answers![indexPath.row]
            // label!.text = "print this"
            label!.text = question.answers![indexPath.row]
        }
        
        
    }
    // return the cell
    return cell
}

There seems to be disconnect between the data and the putting on the screen.

Will try the tag function of each of the screen elements -

I think that is where my problem is.

Fixed it.

// customize it
    let label = cell.viewWithTag(1) as? UILabel
    
    if label != nil {
        
        let question = questions[currentQuestionIndex]
        
        if question.answers != nil && indexPath.row < question.answers!.count {
            // set the answer text
            // label!.text = question.answers![indexPath.row]
            // label!.text = "print this"
            label!.text = question.answers![indexPath.row]
        }

Changed the 0 to 1 in the tableview of the main.storyboard/tableview object.

cell.viewWithTag(1) as? UILabel

Note to self.

Tried to change the tag in the other main.storyboard screen objects. Only works if the label tag is set to 1.
Tried the table view, answer cell, content view, view. All came up with label as descriptor. When I changed the label tag to 1 it worked. I thought there would be some kind of dot notation to reference the storyboard screen object but I guess it doesn’t work like that.

On the M5 L7.

Cannot get the command to popup as I type - resultDialog = storyboard.instantiateViewController(identifier: String)

the instantiate does not automatically fill in like it usually does with Xcode.

Deleted the other view controller. Will start again. Last time used uivew to fill in the object. Will use viewcontroller as search. The icons presented look exactly the same.

Not sure what this means.

Tried to create the labels and buttons to be equal widths on ResultViewController. Appears to be problem in the main.storyboard.

Will recreate.

Fixed the instantiate problem.

Went to main project in the document outline - the very top - has the app icon. General settings - Deployment info. Clicked the up/down arrow - changed it to iOS 13.0.

Tried to type the command again. It pops up automatically like it does in the video.

Broke the code when I deleted the ResultViewController and decided to start from scratch. Looking for solution.

Yeah see I’m thinking about messing with the deployment setting but I’m too chicken to do it lol That’s so weird it actually fixed your missing pop up issue

Note to self.

Trying the Quiz App again. Got the information to load properly. Having problems with layout constraints.

Tried to fix it by recreating storyboard but this broke other things. Got frustrated. Recreating project from scratch. Printed out code before I add storyboard elements so I can simply drop the working code back into the project and start the main storyboard over again if I have too.

Remember with constraints you typically need 4!!

  1. Height
  2. Width
  3. X coordinate
  4. Y coordinate

If you have more or less than 4 you may see constraint errors/warnings

Thanks for the feed back.

Working on a different problem now. Cannot get my iPad to run my project. Popup box says to reconnect iPad. Reconnected iPad. No change. I can see my iPad in the Apple Music app. Ejected iPad. Tried again to run app from Xcode - changed the target device to my iPad ( also lists the available simulators)

Did not work.

Ran the iPad simulator with same generation number. Worked.

update the iPad to ver 15.

Tried again to get the app to run on the my physical iPad. Same error message.

Researching problem.

Which version of Xcode are you using?
You need to have the most updated Xcode for ios 15