: AnyObject to your protocol declaration to restrict it to class types only. This allows weak var delegate: MyDelegate? to compile without requiring @objc. Example: protocol MyDelegate: AnyObject { }.Why this happens
Swift protocols can be adopted by both reference types (classes) and value types (structs, enums). The weak keyword only works with reference types because it relies on automatic reference counting (ARC) to zero out the reference when the object is deallocated. Value types are copied, not referenced, so they don’t participate in ARC and cannot be held weakly.
When you declare weak var delegate: MyProtocol? without constraining the protocol, the compiler doesn’t know whether conforming types will be classes or structs. Since structs and enums can’t be weak, the compiler rejects the code with the error: weak cannot be applied to non-class type 'MyProtocol'.
Historically, developers added @objc to the protocol, which implicitly restricts it to class types (since only classes can be represented in Objective-C). However, @objc brings unnecessary Objective-C interoperability overhead and prevents using Swift-only features like associated types or protocol extensions with Self requirements.
How to fix it
1. Constrain the protocol to AnyObject (recommended)
Add : AnyObject to the protocol’s inheritance clause. This tells the compiler that only classes can conform, making weak references valid.
protocol DataLoaderDelegate: AnyObject {
func dataLoader(_ loader: DataLoader, didLoad data: Data)
func dataLoader(_ loader: DataLoader, didFailWith error: Error)
}
final class DataLoader {
weak var delegate: DataLoaderDelegate?
func load() {
// ... perform loading ...
delegate?.dataLoader(self, didLoad: Data())
}
}
This is the pure Swift solution. It compiles without @objc, preserves Swift-only protocol features, and clearly communicates the class-only requirement.
2. Use a class-only protocol composition (alternative)
If you cannot modify the original protocol (for example, it comes from a framework), create a local typealias that composes the protocol with AnyObject.
protocol FrameworkDelegate {
func didUpdate()
}
// In your code:
private typealias WeakFrameworkDelegate = FrameworkDelegate & AnyObject
class Consumer {
weak var delegate: WeakFrameworkDelegate?
}
The composition FrameworkDelegate & AnyObject creates a protocol type that requires both the original protocol and class semantics. This works anywhere a protocol type is expected.
3. Box the delegate in a class wrapper (last resort)
If you must support struct/enum delegates and need weak semantics for class delegates, wrap the delegate in a class container. This is rarely needed but avoids @objc.
protocol AnyDelegate {
func handleEvent()
}
final class WeakDelegateBox {
weak var delegate: AnyObject?
init(_ delegate: AnyDelegate) {
self.delegate = delegate as AnyObject
}
func callHandleEvent() {
(delegate as? AnyDelegate)?.handleEvent()
}
}
struct StructDelegate: AnyDelegate {
func handleEvent() { print("struct") }
}
class ClassDelegate: AnyDelegate {
func handleEvent() { print("class") }
}
class Owner {
var box: WeakDelegateBox?
}
This approach adds boilerplate and runtime casting. Prefer the AnyObject constraint when you control the protocol.
How to prevent it
- Default to
protocol Name: AnyObjectfor any delegate or callback protocol you define. - Remember that
weakimpliesOptional(var, notlet) — the reference becomesnilautomatically on deallocation. - Use
unownedonly when you are certain the delegate outlives the owner (rare for delegates). - Avoid
@objcon protocols unless you actually need Objective-C interoperability. - If a protocol must support value types, don’t make the property
weak; use a strong reference or redesign ownership.
FAQ
Why does AnyObject allow weak but @objc did too?
Both AnyObject and @objc restrict the protocol to class types. @objc does it as a side effect of requiring Objective-C representation. AnyObject does it explicitly in pure Swift, without the @objc baggage (no selector exposure, no @objcMembers inference, full Swift generics support).
Can I use weak with a protocol that has associated types?
Yes, as long as the protocol inherits from AnyObject. Associated types are a Swift-only feature and work fine with AnyObject:
protocol CacheDelegate: AnyObject {
associatedtype Key: Hashable
associatedtype Value
func cache(_ cache: Cache<Key, Value>, didEvict key: Key)
}
What happens if a struct tries to conform to an AnyObject protocol?
The compiler emits an error: Non-class type 'MyStruct' cannot conform to class protocol 'MyProtocol'. This is intentional — it catches the mistake at compile time rather than allowing a runtime crash or unexpected copy semantics.
Parts of this guide are adapted from content licensed under CC BY-SA 3.0; this article is shared under the same license.
