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 Intermediate Swift Value Semantics, Type Methods and Inheritance Value Semantics

Paolo Scamardella
Paolo Scamardella
24,828 Points

Is this the reason why we don't have or use access modifiers in swift?

Coming from an OOP background with PHP, Java, and C#, it seems that swift does not or need access modifiers because value or reference semantics?

In other languages, we have private or protected keywords to encapsulate data, but after watch this video on value semantics, it doesn't make sense to have access modifiers since we can use the let keyword to control modifying data of an object...does this sound correct in swift?

1 Answer

Simon Di Giovanni
Simon Di Giovanni
8,429 Points

I don’t have any experience with other programming languages, so I’m not sure about the use of access modifiers to ensure that values are immutable.

However with swift, if you have a value type with any ‘let’ constants, any object of that value type will have those immutable values. The same goes for reference types, how they are implemented under the hood is different, but end result is the same. You can’t change a let constant

Access control is definitely used in swift to control access to different parts of code. You have open, public, internal, fileprivate and private. You can read about access control in swift here - https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html

Paolo Scamardella
Paolo Scamardella
24,828 Points

Hi Simon, thank you for the response!