Skip to main content
 首页 » 操作系统

swift之Swift 中的多重继承

2024年01月01日60telwanggs

所以我遇到了一个问题,显示错误

multiple inheritance from classes UIViewController and UIFont

作为编程新手,我真的不明白哪里出了问题。那么这个错误是什么意思呢?我不能向类中添加更多协议(protocol)吗?这是我的代码的样子

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. 
    } 
    */ 
 
} 

请您参考如下方法:

Swift 和 Objective-C 只支持单继承。他们确实支持多种协议(protocol)。扩展主要通过组合实现。

UITableViewDelegate 和 UITableViewDataSource 是协议(protocol)。

UIFont 不是一个协议(protocol),它是一个类。