Reputation: 13
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.
}
*/
}
Upvotes: 1
Views: 3596
Reputation: 112857
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.
Upvotes: 3