Imagine having an app in which the app introduction will no longer be shown on iOS 14 and up. Methods start as being deprecated and will eventually be marked as obsolete. } Marking a method as deprecated or obsoletedĪnother attribute value is used to mark methods as deprecated or obsoleted. You could specify multiple platforms at once as well if 15, macOS 12.0, *) The asterisk indicates the availability of the declaration on all of the platform names listed above unless specified specifically. Options include platforms as well as the swift key to mark pieces of code as available since a specific Swift version. The list of available attributes as of today is as follows: The platform, in this case, iOS, can be replaced too. Obviously, we could replace the number 14 with any OS version that’s available. However, there’s much more we can do with the available attribute in Swift. In the above examples, we’ve only made use of iOS 14 checks. } Possible values for the available attribute We could do exactly the same for methods using the 14, *) Let appIntroduction = NewAppIntroduction() The #available will help us in this case: Two of them will move the problem to a different place in your code by marking the calling class as available since iOS 14 too. Once you’ll try to create an instance of this class inside a project that supports versions lower than iOS 14 you’ll run into the following error:Īn error occurs if you’re creating an instance that is not available.Īs you can see, the compiler will help us to fix this error with a few suggestions. We can demonstrate this by marking a class or method as available since iOS 14, *) Setting the availability for a class or method #available is used to only execute a piece of code for specific platforms and/or versions.is used to mark the availability for a class or method.The shortest answer to describe its difference: We’ve just covered the #attribute which is similar but just a bit different. When navigating through Swift APIs you’ll often run into the attribute. This is great for cases in which you’d like to execute specific code only for a specific iOS version. You can use the available attribute inside a guard statement as well: Print("This code only runs on iOS 14 and lower") Print("This code only runs on iOS 15 and up") For example, if you’d like to execute a piece of code only when it’s iOS 15 and up, you would use the available attribute as follows: Try it today! Checking for an OS version to execute codeĪ basic example comes down to checking for a specific OS version to execute a piece of code. Get more from your QA process Waldo automates the capture of quality debugging information for iOS developers during the QA process, so you don’t have to rely on a vague bug report to address issues in your build.
0 Comments
Leave a Reply. |