Swift question - beginner

Dear community,

I came across a roadblock in one of my exercises from the book "Develop in Swift Fundamentals".

The goal is to create an iOS app about a Quiz, which includes questions, answers and in the end it brings a final screen with results.

The book asks to create different View Controllers, one for the questions and answers, and another one for the results. They are named as following:

QuestionsViewController: For questions and answers

ResultsViewController: For the final result

There is a Segue from the "Questions" to the "Results" view controllers.

Each view controller has its own Swift file with some coding. The error message I'm getting is in the "ResultsViewController" only, after I added the following line of code:

var responses: [Answer]

(ERROR: Cannot find type 'Answer' in scope):

As you can see, it's complaining that the type "Answer" cannot be found in the scope of the "ResultsViewController", however it can be found in the "QuestionViewController", as the screenshot below:

Does anyone know why I'm getting this error message? I appreciate any input. I have tried to get an answer from ChatGPT but with no success.

Thank you, Rodrigo

Answered by rodrigopcn in 767628022

Hi @Claude31 ,

Thank you very much for your help. It worked!

I'm blessed by the power of this community. Hopefully I will be able to help others someday too.

Thanks again, Rodrigo

You have defined Answer struct inside a ViewController. So its scope of visibility is this ViewController alone.

To solve it, Answer must be defined at global level.

So, just move struct Answer before the declaration of QuestionViewController and it will become visible to all classes.

If that answers your question, don't forget to close tyhe thread by marking the correct answer.

Accepted Answer

Hi @Claude31 ,

Thank you very much for your help. It worked!

I'm blessed by the power of this community. Hopefully I will be able to help others someday too.

Thanks again, Rodrigo

Swift question - beginner
 
 
Q