Swift Access Control Explained

  Рет қаралды 33,556

Sean Allen

Sean Allen

Күн бұрын

In this Swift tutorial I explain all the keywords for access control. We walk through private, fileprivate, internal, open and public with examples of each. I recommend using my video as an introduction to get the general idea of how these work, then move on to reading the documentation linked below for further understanding of access control in Swift.
This tutorial was built using Xcode 11 and Swift 5.1.
Access Control Documentation:
docs.swift.org/swift-book/Lan...
If you like my teaching style, I've released my first iOS dev course which is all about the take-home project job interview for iOS developers.
Course Info - seanallen.teachable.com/
Link to my book - How I Became an iOS Developer:
gumroad.com/l/sean-allen-origin
Hired.com:
hired.com/x/1n01g
Check out my podcast, iOS Dev Discussions:
itunes.apple.com/us/podcast/i...
I stream gaming content on Twitch:
twitch.tv/seanallen
Book and learning recommendations that help out the channel if you decide to purchase (Affiliate Links):
Ray Wenderlich Books:
store.raywenderlich.com/a/208...
Ray Wenderlich Videos:
store.raywenderlich.com/a/208...
Paul Hudson's Hacking With Swift:
gumroad.com/a/762098803
Learn Advanced Swift Here:
gumroad.com/a/656585843
Links to my iOS Dev Setup & iOS Dev Book Recommendations
www.amazon.com/shop/seanallen
Personal Links:
Twitter:
/ seanallen_dev
Instagram:
@seanallen_dev

Пікірлер: 66
@seanallen
@seanallen 4 жыл бұрын
Watch Next - iOS Take Home Project - Job Interview Practice - Free Preview - kzfaq.info/get/bejne/g7l5mJWqm6zJlWw.html
@jeffbezos5665
@jeffbezos5665 4 жыл бұрын
I had some doubts after reading the swift documentation, but you just cleared them out for me. Thank you again!
@robinandthedog
@robinandthedog 4 жыл бұрын
Thanks for the attention to this, go this in an interview asked.
@jameelshehadeh9011
@jameelshehadeh9011 2 жыл бұрын
very important topic i got asked about this topic in my last interview, thank you sean
@farziinsan6760
@farziinsan6760 Жыл бұрын
Excellent tutorial! very clear explanation, thanks!
@RizosKonstantinos
@RizosKonstantinos 4 жыл бұрын
This is the video that i was looking for. Thank you Sean.
@seanallen
@seanallen 4 жыл бұрын
Glad you got value out of it 😀
@princekili0422
@princekili0422 3 жыл бұрын
Thanks a lot. This video is really helpful and easy to understand. 👍👍👍
@christostsangaris4785
@christostsangaris4785 4 жыл бұрын
One of the reasons I purchased your “iOS Dev Job Interview practice” without even thinking, is your crystal clear way to explain things! Keep it up Sean!
@seanallen
@seanallen 4 жыл бұрын
Thanks for the kind words, Christos. Hope you enjoy the course 😀
@KFrench1123
@KFrench1123 4 жыл бұрын
Great video Sean, thanks a bunch! Always drill the basics, right 👍
@seanallen
@seanallen 4 жыл бұрын
Thanks Kevin. Glad you liked it!
@valentine1925
@valentine1925 4 жыл бұрын
love it! thanks sean!!
@seanallen
@seanallen 4 жыл бұрын
Happy to help!
@sn3aky_pants593
@sn3aky_pants593 4 жыл бұрын
private(set) is kinda cool too. Gives you read only access, but is private for setters
@andrevdw747
@andrevdw747 4 жыл бұрын
Thanks for clear explanation 👍🏻
@seanallen
@seanallen 4 жыл бұрын
Glad you liked it!
@TheBlueSquirtle
@TheBlueSquirtle 3 жыл бұрын
can u pls do more iOS interview questions? I ran through that playlist and the Swift Keywords playlist and need more of ur content bc this really sticks with ur explanations !! Thanks!!!
@makkah_life_
@makkah_life_ 3 жыл бұрын
Thank you so much
@PatricioBenavente
@PatricioBenavente 4 жыл бұрын
Thanks Sean. I did not understand fileprivate. Love your videos, good work Greetings from Argentina
@seanallen
@seanallen 4 жыл бұрын
Happy you liked the video!
@egesucu9225
@egesucu9225 4 жыл бұрын
File= viewcontroller.swift. You can have many classes under a file like (class view, class Student etc.) fileprivate lets you use a method or a variable inside these classes because they are in the same file(viewcontroller.swift) , but private is private in a class! So you cant call the method that you build inside student class into view class because classes can’t see other classes private methods until you initialize it
@SMIKEex
@SMIKEex 4 жыл бұрын
congrats on 50k subscribers Sean.
@seanallen
@seanallen 4 жыл бұрын
Thanks 😀
@georgeelsham
@georgeelsham 4 жыл бұрын
We need a `groupprivate` I think! Makes it private to a folder/group, similar to Java’s default access modifier.
@JasonMitchellAZ
@JasonMitchellAZ 4 жыл бұрын
Hey look, you hit 50k subs - congrats! 🥳
@seanallen
@seanallen 4 жыл бұрын
Thanks Jason!
@rebeloper
@rebeloper 4 жыл бұрын
Good thing that Sean has left the subscribers count on "open" so we can follow along. Congrats 🎉
@seanallen
@seanallen 4 жыл бұрын
Thanks!
@jasurrajabov
@jasurrajabov 4 жыл бұрын
Thank you Sean for great tutorial about Access Control. As an addition I would also like to know about "final", where and why to use.
@seanallen
@seanallen 4 жыл бұрын
I'm creating a playlist for a lot of the keywords in Swift. "Final" is on my list. You'll see that eventually.
@rebeloper
@rebeloper 4 жыл бұрын
Hi Jasur. "final" is not an Access Control Specifier, it's a Declaration Modifier. It’s applied to a class to indicate that the class can’t be subclassed. It’s applied to a property, method, or subscript of a class to indicate that a class member can’t be overridden in any subclass. Source: docs.swift.org/swift-book/ReferenceManual/Declarations.html#ID381 Hope it helps :)
@AcselRo
@AcselRo 4 жыл бұрын
Apple, please fix the "private" access control to also work in extensions that are in other files, not only in the same file with the declaration. 🤷‍♂️ Great video!
@seanallen
@seanallen 4 жыл бұрын
That would be cool... I'm pretty sure after the back and forth with Access Control that happened form Swift 2 to 4, they've put this topic to bed.
@yixe2253
@yixe2253 4 жыл бұрын
no way, thats going to lead to your code-base being a complete mess!
@AcselRo
@AcselRo 4 жыл бұрын
Yixe Why? It’s pretty common when writting ObjC to move categories into separate files than the parent class.
@omarhussari7666
@omarhussari7666 Жыл бұрын
Thanks Alot!
@seanallen
@seanallen Жыл бұрын
No problem!
@nareshy8379
@nareshy8379 3 жыл бұрын
Hi Allen... I am requesting Please upload setters and getters in swift please do this video Allen
@erickramones2167
@erickramones2167 4 жыл бұрын
do you have a tutorial on how to read a file in ios development?
@aishaoh1209
@aishaoh1209 4 жыл бұрын
Excellent tutorial! very clear explanation, thanks! Also, totally unrelated question but what software do you use to create these videos to essentially have Xcode UI & yourself on the same screen?
@seanallen
@seanallen 4 жыл бұрын
Glad you liked it. I'm recording myself with a Canon M3 camera, recording my screen with QuickTime, and recording my audio with a Blue Yet microphone. Then I edit that all together in Final Cut Pro. You can see more about my setup at seanallen.co/store.
@honza_kriz_bass
@honza_kriz_bass 4 жыл бұрын
Cool video, as always! As you said, it covers the basics more than enough. One little nitpicky thing: I'm sad you didn't include "private(set)" - although I guess it's not a core part of access control, IMO it's a super useful feature! (commenting mostly so that the few people who read comments might hear about it for the first time 😀 don't know if you omitted it on purpose)
@seanallen
@seanallen 4 жыл бұрын
Thanks JK Kross! I did omit that kind of stuff on purpose because I wanted to keep the video focused on the core principle of Access Control. I imagine this video will mostly help beginners and Jr. devs. That's the tough thing about creating tutorials. It's tough to not discuss all the tangential topics, but I don't want to make the video lose focus, drag on forever, or be too overwhelming for viewers.
@honza_kriz_bass
@honza_kriz_bass 4 жыл бұрын
@@seanallen totally understandable! 😎 I'm just that kind of person who loves being bombarded by information 😂😂😂 But I think your reasoning is valid - I watched a few "overwhelming" videos when I was a total noob and it certainly did not help 😂 Only recently I watched some of them again and finally understood 😀
@seanallen
@seanallen 4 жыл бұрын
Agreed. It's tough to make a video that will fit thousands of different people's learning style and preference. I do my best to find a middle ground.
@EmmaKAlexandra
@EmmaKAlexandra 4 жыл бұрын
Really wish Xcode let you set access control in bulk or something. I mostly develop packages & marking relevant variables, methods, and structures public or open can be really clumsy and error prone. There’s also really silly things like struct’s memberwise initializer is specifically marked internal. So I can’t remember a time where I wrote a package, added it to a project and created my own initializer and properly marked it public. Really frustrating!
@iangrant4756
@iangrant4756 4 жыл бұрын
I really wish they’d put a ‘friend’ accessor in Swift, like C++ has!
@stoqnstoqnov7071
@stoqnstoqnov7071 4 жыл бұрын
Hi Sean, great video but it would have been awesome if at the end you have mentioned that you can give specific access control for property setters like for example 'private(set) var isOn' . But its fine, anyone can read it in the link you gave :)
@seanallen
@seanallen 4 жыл бұрын
Glad you liked that video, Stoqn. I omit that kind of stuff on purpose because I wanted to keep the video focused on the core principle of Access Control. I imagine this video will mostly help beginners and Jr. devs. That's the tough thing about creating tutorials. It's tough to not discuss all the tangential topics, but I don't want to make the video lose focus, drag on forever, or be too overwhelming for viewers.
@stoqnstoqnov7071
@stoqnstoqnov7071 4 жыл бұрын
Sean Allen yes Sean you are absolutely right, i bet there are thousand more details that people can point out, but what is the purpose of getting beginners lost in all that ? And after all the more your videos are targeted to a broader audience the better the growth of your channel will be 🙂 Before a couple of moths i tried the exact opposite - a channel showing a really technical stuff and got no views at all kzfaq.info/get/bejne/l9qgdcJ-xqerdp8.html So keep up the good work mate, i would say you are doing the best job keeping the ios devs up to date on youtube :) Wish you all best, Stoyan from Bulgaria
@seanallen
@seanallen 4 жыл бұрын
Thanks for the kind words!
@gakkieNL
@gakkieNL 4 жыл бұрын
Online tutorials should use and teach access control more often. Part of proper coding imho... thanks for the video!
@seanallen
@seanallen 4 жыл бұрын
Glad you liked it, Gakkie!
@rebeloper
@rebeloper 4 жыл бұрын
Agreed. For example not many know that in SwiftUI you should always set your @State variables to be private
@thewalkerthapa5435
@thewalkerthapa5435 4 жыл бұрын
Thank you. Can you make one video on subscript?
@seanallen
@seanallen 4 жыл бұрын
Eventually.
@aknurasatenova4419
@aknurasatenova4419 3 жыл бұрын
Is this encapsulation?
@teakodo
@teakodo 4 жыл бұрын
YES
@seanallen
@seanallen 4 жыл бұрын
Glad you liked it!
@andreagrumpeenlate5813
@andreagrumpeenlate5813 4 жыл бұрын
Is there an advantage to using private? Is performance affected by degree of access of your code components?
@rebeloper
@rebeloper 4 жыл бұрын
Not that much of a performance boost, but you certainly will help the compiler out if you use private wherever it's appropriate. For example in SwiftUI you should always set your @State vars private.
@mediterrenean
@mediterrenean 3 жыл бұрын
it seems that Sean was conflating private and fileprivate for the first 4 minutes
@mazedar_tv
@mazedar_tv 2 жыл бұрын
So whats difference between Public and Internal?
@seanallen
@seanallen 2 жыл бұрын
Public is exposed to outside projects. For example if you were to import a 3rd party library or SDK, they would need to mark their functions as public so your project would have access to them. Internal is exposed to everything within that specific project.
@davidcbeaudoin
@davidcbeaudoin 2 жыл бұрын
Bit of a nitpick, but early on you keep saying you don't want files outside of the example file to have access to the private property. I think you mean other classes, or even better, outside code.
Swift Closures Explained
14:23
Sean Allen
Рет қаралды 62 М.
How to use Access Control (Private vs Public) | Swift Basics #12
18:59
Swiftful Thinking
Рет қаралды 7 М.
IQ Level: 10000
00:10
Younes Zarou
Рет қаралды 12 МЛН
Doing This Instead Of Studying.. 😳
00:12
Jojo Sim
Рет қаралды 23 МЛН
Exploring Linux: Types of Linux Operating Systems Explained!
55:09
TechWorld With Hari
Рет қаралды 2
How to use Lazy in Swift
9:56
Sean Allen
Рет қаралды 39 М.
Swift Optionals - How to Unwrap (real examples)
14:20
Sean Allen
Рет қаралды 12 М.
Filter, Map, Reduce - Swift - iOS Interview Questions
11:08
Sean Allen
Рет қаралды 53 М.
Memory Leaks in iOS: Find, Diagnose, & Fix (2022)
12:44
iOS Academy
Рет қаралды 48 М.
Weak and Unowned Self Closure Memory Leak Fixes
12:21
Lets Build That App
Рет қаралды 56 М.
iOS Developer Mock Interview | Tech Round (Round-2)
31:37
The Story of Next.js
12:13
uidotdev
Рет қаралды 560 М.
ноутбуки от 7.900 в тг laptopshoptop
0:14
Ноутбуковая лавка
Рет қаралды 3,6 МЛН
تجربة أغرب توصيلة شحن ضد القطع تماما
0:56
صدام العزي
Рет қаралды 64 МЛН
Tag him😳💕 #miniphone #iphone #samsung #smartphone #fy
0:11
Pockify™
Рет қаралды 4,7 МЛН
📱магазин техники в 2014 vs 2024
0:41
djetics
Рет қаралды 694 М.