Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

when we declare stored properties, we don't give it any initial values ...

In the video Pasan stated:

Well, when we declare stored properties, we don't give it any initial values so 5:44 the compiler cannot infer what the type should be. 5:47

But that is not entirely true is it? We could set default values as shown below:

struct Town {
    var population = 5_422
    var numberOfStoplights = 4
}

1 Answer

Hello,

Yes, you could set default values for the stored properties in your classes or structs. However, we assign default values to a stored property only if we are sure that the value would occur most of the times, to avoid having to type the same value over and over again. The convention is to declare the data type of the property in the class or struct. Now, if you create an instance of the struct Town, Xcode autocomplete will show two possibilities, the default (with no properties), and the one with both the properties within a parenthesis. By clicking on the latter, you can edit the values in them. Remember, they can only be integer values.
Hope that helps.