9

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?

J2K
  • 1,593
  • 13
  • 26
  • I haven't seen other way around. I think we have to use something like @available(*, unavailable, message: "Err msg") – Bubu Feb 08 '19 at 19:33
  • Does this answer your question? [How to manually deprecate members](https://stackoverflow.com/questions/25405133/how-to-manually-deprecate-members) – Axel Guilmin Jun 04 '22 at 14:21

1 Answers1

8

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

You can read up on it here under Declaration Attributes.

enter image description here

CodeBender
  • 35,668
  • 12
  • 125
  • 132
  • Is there a way to accomplish this without having to define init(frame:)? For example, in my class, I don't want anyone to call init(). My class uses init(), but it wants init to be private. So I can do: private override init() { super.init() } But I really want to do this like NS_UNAVAILABLE, without having to provide an implementation for init(). – mahboudz May 15 '20 at 21:53