# Class-only Protocols - class or AnyObject

SwiftMic

iOS 系统中广泛使用了 delegate 模式，如果有 Swift 开发经验的话，你将会发现如下代码无法正常通过编译。

﻿

class MyClass {  weak var delegate: MyDelegate?}protocol MyDelegate {}

﻿

﻿

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

﻿

﻿

protocol MyDelegate: class {}

﻿

﻿

﻿

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

﻿

﻿

protocol MyDelegate: AnyObject {}

﻿

﻿

﻿

﻿

﻿