Public vs. Private in Swift 5 - Access Control (Xcode 11)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] what is going on everyone welcome back to another Swift tutorial today we're gonna be doing something slightly different from what we traditionally do on this channel which is building stuff and that is taking a look at access control so here we have the different access controls that Swift provides so a public private file private open and internal so we're gonna look at open public internal and private I'll be skipping file private as it's not as common so this video is actually inspired by someone who left a comment on earlier video this week and I've noticed that most people don't explain what this is and kind of just assume that it makes sense so this is gonna be a pretty quick video and it might be a little all over the place because it's a lot of explanation but I hope you guys enjoy these things are when they should it shouldn't be used because it's super prevalent in industry professionally and it's helpful for your own projects as well so that said make sure you absolutely smash that like button down below helps out that YouTube algorithm subscribe if you're new get excluded and you get excited let's talk about some access control quick pause before we get into the video if you haven't seen it already I am hard at work putting together iOS Academy i/o a community where all of us iOS engineers can come together learn how to build some of the top apps like Facebook YouTube and Instagram in addition to interview prep to learn some of these iOS roles at top tech companies so if you're interested in the free and premium content to come head on over to iOS Academy IO and enter your email address into the wait list forum and you will be notified as content becomes available that said let's get into the video right into it so let's create a project let's stick with a single view application and we'll call this access control and let me save this to my desktop expand Xcode and jump right in so this video is actually inspired by one of you guys so someone left a comment asking some access-control questions and I realized that even here on YouTube most people might briefly mention public-private but no one really has gone in-depth as to what it is and it's super super important so I figured why not create a video on it so that said we're gonna start in our viewcontroller that is created for us by defaults in this template so you'll notice that in here we have just this function which is overridden and there's no public private prefixed keyword so you might have seen that there are functions that are both prefixed public and there's also functions that have no prefix and then similarly for private open and it'll be copy and paste this so you might have come across functions like this and open so let's talk about each of these and what they are what they do and what they're used for and then we're gonna do some examples so the first thing I'll mention is here I've used functions but everything we're about to go over also applies to properties so for example if you had a public VAR thing with the string and so on and so forth for all these other ones I won't type it out just because it's redundant so the names are a little indicative of what they are so public here means that this function or a property with the prefix of public will be accessible should this class be instantiated somewhere else so if you import this view controller somewhere else and create an instance of it you'll be able to call this function so conversely private is the option if you create an instance of the zoo controller somewhere else and you try to call this food to function the autocomplete won't actually show it to you and the compiler will give you an error because it's not aware that futu exists and the reason that it's not aware is we've explicitly called it private so moving on to open opens a little more complicated a little bit not not bad a lot so open and public are very similar they both allow access and the public namespace a public domain so if you import this controller somewhere and want to use this foo 3 you absolutely can the extra bit that open gives you and what differentiates it from public is the fact that you can override open things if it's a function or a property so you'll notice here we have this override keyword for this viewdidload so if you actually click on this view did load which is super view dat loading a hold command to click it and click into it you'll notice that this function is open so this is the Apple written like internal SDK that they ship and it contains all of these things and a lot of these are open and they're opens hence we can override it in our own subclass implementation of this view controller and the last one is this function or a property that has no prefix and this is actually equivalent to if we had internal so internal or not having it basically means that it's it's similar to private but it's only accessible within the framework that you're working in so for example we have UIKit here so if we go into UIKit well there's a bunch of things in here so i'm not going to actually click into these but there are some things in UI kit that are not explicitly marked as open or public so we the consumers of uikit the framework cannot use those properties or functions so that's why most people they will just leave a function written as such and not give it a access control level at all an access control is literally just what these prefixes are so if anyone ever uses that term that's what that is so that all said let's comment this jazz out and let's do some examples and see kind of why in a real world setting we might want to use one or the other so what we're gonna first do is let's demonstrate public in private so let's create a new file and we'll stick with the Swift class or a swift file and we'll call this math calculator and here let's create a class called math Compu let's call it something better actually let's call it I don't know math doer that sounds technical enough and actually something I forgot to mention is public private and all the scope level stuff can also apply to other declarations not just properties and functions but things like a class might be public or if you have a struct it might that might be public so keep in mind that there's access control behavior does apply to a number of things so but all the rules we mentioned about what's available what's private what's internal that all holds true for all of these things that can be prefixed and annotated with an access control so because we're still working in this app target we don't need to explicitly mark this as public but what we're gonna do in here we are gonna create a public function called perform within X Y and it'll spit out an int as a return value and now we're gonna have a private function called add which takes an X Y and also returns an int in perform we're simply going to return the result of ADD and we're getting out basically proxy in the x and y parameters here and here we'll return X plus y so now what you be asking what's the value of doing any of this and it looks like we have an error because I clearly can't type today so the value in doing this is if you're reading a class where you just want to arbitrarily call some perform function you can off you skate or hide what's happening under the hood in this private logic so this function essentially just calls this private function but the caller that is using this math doer class is not aware of what's going on here all it knows is that it can perform so if we head back to the view controller and in here if we create an instance of this class so let's just call it object is this you can see on object if we type we try to add it doesn't even show up in the autocomplete but if we say pin perform it shows up and let's just say that the result of this is some and we'll print out some so let's say command R and you'll notice that the addition does in fact work once this decides to load but the key takeaway here is that the actual function which is doing the addition is hidden in a private function so there's our results so that's a look at public in private I think public in private is particularly the simpler it's pretty indicative of the terms of what they're used for the things that I think are more interesting are open and internal so for open we're gonna actually create a framework so a framework and how is a bunch of things let's say we have a framework that does some type of computation so we have a dedicated framework for it so we can put things there that are open that we can override in the view controller like this viewdidload function and then we could also put things in there that are public that we can use but can't override so let's do that so file and we want to hit new and we want a new target and if you're not familiar with how to create frameworks or what this is for the purposes of the video I would just take away that you can create these frameworks this way and what the application is for the sake of public and private and open in other words the actual framework creation is slightly irrelevant here so once we've created this target you'll see that we have a fou framework here and up here we can also switch to fou framework and it compiles if we switch to it and hit command B let's go back to the app target go to the view controller let me get rid of all these comments I'll to also get rid of this and in here we can import food framework and right now that framework is empty but let's go and add stuff to it so let's create a new file and we'll just call this example dot Swift and let's do some stuff in here the first thing we're gonna do is we're gonna create a open class and we'll call it vehicle and a vehicle let's say has a property called color and it's a UI color let's say it's white now we need to import UI kit because it's a color and colors are part of UI kit now let's also create a public class called car in here we want to add a public function called Drive we're also gonna add a private function call stop and we also need to add public initializers to both of these classes so we can simply do a public in it leave it empty and let's copy and paste it and put this down here as well and I believe that's all we need in this framework so if we select the framework up here hit command B everything should build and let's go back to the app target go to our review controller and let's try to use these two classes we just created so vehicle is open and car is public so we should be able to instantiate both of them here so a car is a car and V is a vehicle now if we hit command B we shouldn't have any errors but it looks like we do so those first ones a warning because we haven't used it and we have an error here that we even issued with the equals oh that's weird we should put a space before that equal now it's building we have warnings that we've instantiated these two elements these two objects but aren't using them so if we come down here let's try to use the functions on car so you'll see we can do car drive but we're not going to be able to do car dot stop and in fact it should even tell you in the error that stop is inaccessible due to private so the compiler is aware that stop exists in here but it's not going to let you call it because we've explicitly said that it's private and similarly we can definitely access this color property to on a vehicle so if we go back to the controller and and we say current color is V dot color you'll notice that it's highlighted properly in green and we are able to access it again you can ignore this warning it's just saying we haven't used it but what's more interesting about open is we can create another class and so we call it boat and we can actually inherit from vehicle and the reason we're able to do this and if you hit command B you'll see things compile is because vehicle is in fact open but if we take the color class and let's say we create B of W and we try to inherit from car you'll see we will get an error here and the reason that we are getting an error here if we take a look at the message is cannot inherit from non open class car and what is that telling us that's telling us at the car class in the framework here is not open so it is in fact public so we can access it and we can access its initializer and drive not the stop function but we cannot inherit from it and consequently we cannot override any of the functions or properties or any of that stuff so that is a very quick overlooked overview of open public private and internal so this video is a little all over the place so I hope I was as clear as I could be with these access control prefixes if you have any questions or if you're confused about part of it don't hesitate to leave a comment down below if you haven't smash that like button yet make sure you do so subscribe if you're new I do Swift tutorials other tech videos thanks for watching and I'll see you in the next one
Info
Channel: iOS Academy
Views: 2,919
Rating: 4.9703703 out of 5
Keywords: swift 5, access control, swift public private, swift tutorial, swift beginners, how to make app beginners, learn swift, swift programming, frameworks ios, swift app, swift project, xcode 11, swift 2020, create app in swift, first swift app, swift lesson, uitableview, swift public, swift internal, swift open, public functions swift, functions swift, properties swift, variables swift, constants swift, swift versions, swift errors, swift 5 tutorial, access control swift
Id: 9FWRMp5TgAk
Channel Id: undefined
Length: 16min 43sec (1003 seconds)
Published: Wed May 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.