当前,我们有一个协议和一个协议扩展:
protocol Camera: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func openTheCamera()
}
extension Camera where Self: UIImagePickerController {
func openTheCamera() {
if UIImagePickerController.isSourceTypeAvailable(.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .camera
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
}
}
由于该函数已在协议扩展中定义,因此我尝试直接在符合条件的类中调用它:
class SomeClass: Camera {
override func viewDidLoad() {
super.viewDidLoad()
openTheCamera()
}
}
问题是,当我尝试使该类符合条件时出现错误:
Typical UIViewController不符合协议“ Camera” 您要添加协议存根吗?
问题是func已经在扩展中定义了,我不想再将它添加到控制器中。如果我添加协议存根(openTheCamera(){})并在控制器中保留为空,那么我在协议扩展中定义的功能将无法执行。
答案 0 :(得分:0)
procotol条件错误。您应该使用:
extension Camera where Self: UIViewController {