4.6 Availability Checking
Swift 2 has introduced a special construct that can be used to test for a minimum version of the OS X, iOS or watchOS platform. In this lesson, I'll show you how to use the
#available keyword to ensure that specific features or APIs are available when your app is run.
1.Introduction3 lessons, 15:05
2.Foundational Concepts5 lessons, 43:51
3.Working With Collections3 lessons, 26:26
4.Controlling Flow7 lessons, 1:22:24
5.Functions5 lessons, 51:54
6.Creating Types6 lessons, 1:08:10
7.Conclusion1 lesson, 01:15
4.6 Availability Checking
Another interesting feature that was added to Swift 2 very recently, was this concept of availability checking. And this is a very interesting feature, and I put it into the flow control section of this course mainly because, well, that's actually what it's used for. So, in the past, if you were trying to target some of the latest and greatest capabilities that were released with the latest versions of say, the iOS SDK or the OS X SDK, you kinda had to make a decision. You either had to go full force and say that you were only gonna support that platform, or be on that. Or, you were going to support that platform, but then you were also gonna have to try to find some kind of kludge way of determining what version of the platform or what version of the SDK you are on. And then if you weren't on at least that level, by using some sort of checking if a selector existed, for those people who have done that In the Objective-C world. Then, you were able to kinda stitch something together if that wasn't found, so we kinda had this opportunity to use some availability if it was there. But if not, we could kinda fall back, and there's been a new feature added to the Swift programming language that's gonna help us do this, natively without us having to kinda put this kludge solution back together. And it's actually quite simple. And it's gonna fall within an if statement. And then after our if statement, we're gonna use the pound or hash tag and they were going to type available. So now within here, this is basically a conditional check, and now we're gonna have open and closed parentheses. And then within here we're going to define the platforms or the minimum version of the platform that we want to check for, if we want to be able to use some piece of functionality. But if it doesn't exist, it at least gives us a way to know that, so then we can go ahead and fall back to some other way of doing it. Now it's quite simple to use. All we're going to do within the parentheses, is define a comma, separated list of the minimum versions of either the iOS or the OS X or even the watchOS platforms. That we want to specify that we need In order to actually use this piece of functionality. So I can put in here things like, I want to check for the iOS platform, maybe version 9 at least. And I could look for the OS X platform, maybe at version at least ten. And then I can even look for watchOS, and maybe look for at least at platform level two. Now, I can continue to put these in these things in here just like this and I can even get down into the major and minor versions. Maybe 9.9, or I could come into OS X and we could look for at least 10.7. We could continue do this and get very granular with what we're looking for, but the interesting thing is we can specify one, two or all three of these if we want to. But at the end, we have to specify kind of a wild card. So let's say I'm running, or I'm writing this application to run on OS X. And I wanna make sure that this feature exists that I wanna use, and it was released in version 10.10 of OS X. Now, I can't just check for this, because this would say, I'm only wanting to know if I'm running on this version. And I want to run in this version, it's got to be at least this version for me to use this piece of functionality. But then, I also wanna be able to be able to run and use this functionality beyond that as well. So I would have to specify a comma here. And the last argument that I'm gonna have to put into this if statement, within the hashtag available, is gonna be the star. And what that says, is I'm going to support all subsequent versions of either just this platform, or all the platforms that I specify within here. Once I've done that, then I can do my open and closed parentheses, just like I've done with any other conditional statement. And I can put in my platform specific code in here. But in this case, I'll just write a print, and I'll just say, I support this version. And then you could come down and you could do subsequent if else statements here. Maybe if you wanted to use a piece of functionality that was available in 10.10, I could run it here. I could check for an else if, maybe if I was running on at least 10.8, I could use another function or another piece of functionality. And then I could drop in an else, and I could just put in my else here, print("I don't support much"). Or something along those lines, so I can go ahead and say that. Now in this particular case we're gonna get a warning. And this is because it's saying that this is an unnecessary check. Because the minimum deployment target that we've selected, or that was created for us when we created this project, already ensures that this is going to be true. But regardless, it's going to be okay. So basically, what that's saying, is that this statement is always going to be true. But I could go ahead and save this and run it, and what you'll see here is that it says, I support this version because this is a true statement because the platform OS X 10.10 is available. So I'm gonna go ahead and print that line. Now if I were to maybe put this up to something bigger, say,10.12, I could go ahead and say this. And now that goes away because this is beyond the supported version or beyond the version that I'm running on. And I could go ahead and run this now, and it's gonna say, I don't support much. So that's a nice little feature added into the Swift language that we can now use. That is for the specific reason of checking for the availability of certain pieces of functionality that we might want to use within our application. And then be able to fall back on some other solution, a little bit more gracefully.