【发布时间】:2021-05-27 21:32:19
【问题描述】:
当我们向协议声明可选属性时,需要用@objc 属性对其进行标记。
协议被限制为UIViewController类型的类。
@objc protocol User: UIViewController {
@objc optional var userImage: UIImage {get set}
}
我收到以下错误。如何将协议限制为UIViewController 并具有可选属性?
error: inheritance from non-protocol type 'UIViewController'
@objc protocol User: UIViewController {
【问题讨论】:
-
这里有些不对劲,
User怎么可能是视图控制器的子类?你的意思是UserController? -
编译时会出现警告
Redundant constraint 'Self' : 'AnyObject',当我们尝试通过实现的控制器访问时,会出现编译时错误Value of type <Implementing Controller>, has no member "userImage"
标签: ios swift swift-protocols