简体   繁体   中英

What is the Swift equivalent of NS_UNAVAILABLE?

In Objective-C we can mark certain methods as NS_UNAVAILABLE meaning we will get a compiler level error if there is an attempt to call them. This can be useful when a sub class wants to reduce the scope of the api of the superclass it inherits from. For example a new UIView subclass might want to enforce that it has to be created via a nib and thus might mark initWithFrame as unavailable.

Swift has the @available marker but is there a simple way to mark a method is unavailable similar to NS_UNAVAILABLE in Objective-C?

You can use @available by marking the function as @available(*, unavailable) .

You can read up on it here under Declaration Attributes .

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM