Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: Multiple Inheritance from classes Error UIViewController and UIIMagePickerController

I manually added two additional controllers (UINavigationControllerDelegate & UIImagePickerController) to a UIViewController and I am receiving an error after adding the UIImagePickerController that says,

Multiple inheritance from classes UIViewController and UIImagePickerController

I'm not sure how to interpret and fix that right now.

As a result of this error, I'm also seeing one when I use the image.delegate method and set it equal to self

Type ViewController does not conform to protocol UIImagePickerControllerDelegate

Code:

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerController {

    @IBAction func pickImage(sender: UIButton) {

        var image = UIImagePickerController()
        image.delegate = self
        image.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        image.allowsEditing = false

        self.presentViewController(image, animated: true, completion: nil)


    }
like image 226
cphill Avatar asked Feb 19 '15 03:02

cphill


People also ask

Can a class inherit from multiple classes Swift?

Swift doesn't allow us to declare a class with multiple base classes or superclasses, so there is no support for multiple inheritance of classes. A subclass can inherit just from one class. However, a class can conform to one or more protocols.

Which inheritance is not supported in Swift?

Multiple inheritance is not possible in Swift.


2 Answers

You should have UIImagePickerControllerDelegate, not UIPickerViewController in your first line. The system thinks that you are trying to have your controller inherit from both UIViewController and UIImagePickerController which is not allowed.

like image 172
rdelmar Avatar answered Oct 13 '22 16:10

rdelmar


I think it is that swift doesn't allow multiple inheritance, protocols are essentially what other languages call interfaces and they serve the same purpose - to allow a safe and limited form of multiple inheritance. Try to change it like this. See more from here and protocol.

class ViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate
like image 45
gabbler Avatar answered Oct 13 '22 16:10

gabbler