3

Class-only Protocols: class or AnyObject

 3 years ago
source link: https://sarunw.com/posts/class-only-protocols-class-or-anyobject/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Class-only Protocols: class or AnyObject

09 Mar 2020 ⋅ 2 min read ⋅ Swift Protocol

Table of Contents

The delegation pattern is widely used in the iOS system. If you are an experienced Swift developer, you know that the following code won't work with this pattern.

class MyClass {
weak var delegate: MyDelegate?
}

protocol MyDelegate {
}

You will get compiled error like this:

'weak' must not be applied to non-class-bound 'MyDelegate'; consider adding a protocol conformance that has a class bound

To make a protocol work with weak keyword, you have to limit a protocol adoption to class types.

protocol MyDelegate: class {

}

If you declare class-only protocol like the example above, I assume you have been writing in Swift for too long, maybe far too long. You might not realize that : class is no longer a recommended way of declaring class-only protocol.

From Swift 4, class keyword is deprecated. Even the compiler doesn't give out any warning at the moment.

This proposal merges the concepts of class and AnyObject, which now have the same meaning: they represent an existential for classes. To get rid of the duplication, we suggest only keeping AnyObject around. To reduce source-breakage to a minimum, class could be redefined as typealias class = AnyObject and give a deprecation warning on class for the first version of Swift this proposal is implemented in. Later, class could be removed in a subsequent version of Swift.
SE-0156 Class and Subtype existentials

You should declare it with AnyObject instead.

protocol MyDelegate: AnyObject {
}

What should I do?

Replace all occurrences of class-only protocols from class to AnyObject and try to get used to a habit of using AnyObject from now on.

sponsor-codeshot.png

Why should I bother?

Even class does not give out compiled error yet; I think it might in the future. And even though it won't, for better communication and understanding among your team, you should start using AnyObject. Because class is no longer present in official Swift documentation and a new generation of Swift developers might never know it exists.

Old habits die hard, but it has to die.

Updated Xcode 12.5 finally shows a warning about this 🎉.

Reference


You may also like

How to declare Swift protocol for a specific class

Learn how to create protocols that constrain their conforming types to a given class.

Swift Protocol
How to get the first N elements of array in Swift

Learn a few ways to do it and things you should know when using them.

Swift
How to convert a String to an Int in Swift

Learn to convert a string "123" to an integer 123.

Swift

Read more article about Swift, Protocol,

or see all available topic

Get new posts weekly

If you enjoy this article, you can subscribe to the weekly newsletter.

Every Friday, you’ll get a quick recap of all articles and tips posted on this site — entirely for free.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron

Tweet

Share

Previous
How to create Neumorphic design in SwiftUI

Neumorphism or Neomorphism is a new design trend of UI recently. We are going to see how to implement this in SwiftUI.

Next
How to create code snippets in Xcode

Create a reusable boilerplate snippet that you can use in the project.

← Home


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK