How to use the #available property in Swift

Tram Ho

Marking the code snippets as available to each platform or version is essential in the ever-evolving context of an ever-evolving application. as soon as possible. If it does not have support for older versions, we can use the available property in Swift.

1. Check the operating system version to execute the code

A basic example of testing a specific operating system version to execute a piece of code.

For example, if you only wanted to execute a piece of code that was iOS 14 or later, you would use the available property like this:

You can also use the attribute available inside the guard statement:

This is great for situations where you only want to execute code for a specific iOS version.

2. The difference between available and #available

When navigating through the Swift APIs, you will usually encounter the available property . We just mentioned the # property which is similar but only slightly different. The shortest answer to describe its differences is:

  • available is used to mark the availability for a class or method
  • #available is used to only execute a piece of code for specific platforms or versions

3. Set availability for a class or method

We can demonstrate this by marking a class or method as available since iOS 14:

When you try to create an instance of this class inside a project that supports versions lower than iOS 14, you get the following error:

As you can see, the compiler will help us fix this with some hints. Two of them will move the problem to another place in your code by marking the class called available since iOS 14. #available will help us in this case:

We can do the same for methods using the available property:

4. Possible values ​​for the available property

In the examples above, we only used it to test iOS 14. However, we can do a lot more with the available property in Swift.

Obviously, we can replace the number 14 with whatever OS version is available. The platform, in this case iOS, can also be replaced. The list of properties available until today is as follows:

  • iOS
  • iOSApplicationExtension
  • macOS
  • macOSApplicationExtension
  • macCatalyst
  • macCatalystApplicationExtension
  • watchOS
  • watchOSApplicationExtension
  • tvOS
  • tvOSApplicationExtension
  • swift

Options include the foundation as well as the swift keyword to mark code snippets as available since a particular Swift version.

The asterisk indicates the availability of the declaration on all platform names listed above, unless specifically specified. You can also specify multiple platforms at the same time if required:

5. Mark a method as unavailable or removed

Another property value is used to mark methods as deprecated or rejected. The started methods are deprecated and will eventually be marked as rejected. Imagine there is an app where the app trailer will no longer be visible on iOS 14 or later. You can mark a specific method if it was used from the SDK as unusable and removed as follows:

When implementing the code, they get the following error:

The version numbering using these values ​​is often confusing. In the code example above, you can think that this method is deprecated on iOS 12 and has been removed in iOS 13. However, it is read differently:

  • This method is not used in versions higher than X
  • This method is removed in versions higher than X

The message is used to describe the reason and can be helpful to explain the change to implementers.

6. Mark a method as renamed

When developing an SDK for open-source or another user, you may want to move the implementer to your newer coding methods. In these cases, you can use the renamed property:

Note that we first mark a method as unavailable. The renamed value indicates which method should be used instead.

Xcode will help implementers in a unique way by showing our renaming fix option:

7. Negative statement available

A common question when working with the available attribute is to negate the statement and write code like: “Only run this if the iOS version is lower than X”.

This will be done as follows:

8. Conclusion

We have covered all of the possibilities for using properties available in Swift. You can run code for Swift and platform specific versions, and you can now mark methods as deprecated, discarded, or renamed.

Hopefully, this will help you with more efficient coding.

So my post here is over. Thank you for watching the article.

Share the news now

Source : Viblo