2

I'm currently reading through Apple's "Intro to App Development with Swift" student guide. At the end-quiz of chapter 9, "Types", it asks:

When you're reading code and aren't sure of the type of a variable or constant, what's the quickest way to find out?

The answer is:

Rewrite the section of code using different encapsulation.

However, the concept of encapsulation has not been introduced before so I'm rather confused. In the e-book's Glossary, it states:

A language mechanism for restricting access to some of the object components and/or a language construct that facilitates the bundling of data with the methods of operating on that data.

Can anyone explain how this relates to finding out about the type of a variable or constant?

Thanks in advance.

1 Answer 1

1

The answer is:

Rewrite the section of code using different encapsulation.

That's a wrong answer. The right answer is "Use Xcode's [Quick Help] inspector."

Can anyone explain how this relates to finding out about the type of a variable or constant?

The suggestion here is that if you rewrite the source to make a private variable public (i.e. change the way it is encapsulated) it would make it easier to find the type of the variable in question.

Sign up to request clarification or add additional context in comments.

2 Comments

Actually, the best way is to select the variable in question and look at the Quick Help panel. The type of the variable is displayed. Jump to Definition won't help if you haven't got an explicit type declaration for this thing, and very often, you don't. But Quick Help displays the inferred type.
Right, it looks like Apple made a mistake here. The correct answer is 'B. Option-click the constant's or variable's name in Xcode', but it gets marked wrong. This concept is taught on page 7 of chapter 9, where it says, 'Hint: Check the type by holding down the Option key while clicking the anotherString constant.'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.