1

So I have a problem where I get shown the error

multiple inheritance from classes UIViewController and UIFont

and being new to programming, I don't really understand what is wrong. So what does this error mean? Can't I add more protocols to the class? This is how my code looks

import UIKit
import Foundation

 var items:[String] = []

class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIFont {


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

        var cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "cell")
        cell.textLabel?.text = items[indexPath.row]        
        return cell
    }
    func fontWithSize(fontSize: 130) -> UIFont {
        return fontSize
    }



    override func viewWillAppear(animated: Bool) {
        if var storeditems: AnyObject? = NSUserDefaults.standardUserDefaults().objectForKey("items") {
            items = []
            for var i = 0; i<storeditems?.count; ++i {
                items.append(storeditems?[i] as NSString)
            }

            func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

                if editingStyle == UITableViewCellEditingStyle.Delete {
                    items.removeAtIndex(indexPath.row)
                    NSUserDefaults.standardUserDefaults().setObject(items, forKey: "items")
                    NSUserDefaults.standardUserDefaults().synchronize()

                }
            }
        }
    }
}

View Controller 2

import UIKit

class ViewController2: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var textField: UITextField!

    @IBAction func button(sender: AnyObject) {

        items.append(textField.text)

        NSUserDefaults.standardUserDefaults().setObject(items, forKey: "items")
        NSUserDefaults.standardUserDefaults().synchronize()

    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
0

1 Answer 1

3

Swift and Objective-C only support single inheritance. They do support multiple protocols. Extension is principally achieved via composition.

UITableViewDelegate and UITableViewDataSource are protocols.

UIFont is not a protocol, it is a class.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried working that, but that doesn't work. It still says multiple inheritance from classes. What i am actually trying to do is add another protocol named UIFont to edit the font style.
There is not protocol named UIFont, that is a class. Why do you thing you need to include it as a protocol?
Yeah my bad, UIFont is a class, i am pretty new to programming let alone OOPs. Having a hard time finding my way through this
You write "OOPs", I write "Oops" :-;
Actually, there are 2 types of inheritance: inheritance of interface and implementation. So, both Swift and Objective-C do support inheritance :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.