![]() Using the unavailable attribute is cleaner than the code you had to write before Swift 5.6: The new API is unavailable if the OS is lower than iOS 15 Swift 5.6 introduced the #unavailable attribute in SE-290 allowing us to invert the available check as follows: Only run this if the iOS version is lower than X Unavailability Condition using #unavailableĪn often asked question when working with the available attribute is to negate the statement and write code like: Xcode will help implementors nicely by showing a fix option for our renaming: The renamed value indicates which method should be used instead. Note that we’re marking a method as unavailable first. In these cases you can make use of the renamed unavailable, renamed: "launchOnboarding") When developing SDKs for colleagues, open-source, or other users you might want to migrate implementors to newer methods of your code. The message is used to describe the reasoning and can be helpful to explain the change to your implementors. This method is obsoleted in versions higher than X.This method is deprecated in versions higher than X.In the above code example, you might think that the method is deprecated on iOS 12 and obsoleted on iOS 13. The version numbering when using these values is often confusing. When implementers of our code still try to use our methods they will run into the following error: You could mark the specific method if it’s used from an SDK as deprecated and obsoleted as deprecated: 12, obsoleted: 13, message: "We no longer show an app introduction on iOS 14 and up") 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: Learn more! 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. Waldo helps teams like Rocket Money provide bug free experiences to their iOS users while saving time. Save 250+ hours on mobile testing When it comes to testing, efficiency and coverage should not be mutually exclusive.
0 Comments
Leave a Reply. |