【问题标题】:Multiple inheritance in SwiftSwift 中的多重继承
【发布时间】:2016-12-18 22:27:18
【问题描述】:

所以我遇到了显示错误的问题

从 UIViewController 和 UIFont 类的多重继承

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

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()

                }
            }
        }
    }
}

视图控制器 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 multiple-inheritance


    【解决方案1】:

    Swift 和 Objective-C 只支持单继承。它们确实支持多种协议。扩展主要是通过组合来实现的。

    UITableViewDelegateUITableViewDataSource 是协议。

    UIFont 不是协议,它是一个类。

    【讨论】:

    • 我尝试过这样做,但这不起作用。它仍然说来自类的多重继承。我实际上想要做的是添加另一个名为 UIFont 的协议来编辑字体样式。
    • 没有名为UIFont的协议,那是一个类。为什么您需要将其作为协议包含在内?
    • 是的,我的错,UIFont 是一个类,我对编程很陌生,更不用说 OOP。很难找到解决办法
    • 你写“OOPs”,我写“Oops”:-;
    • 其实继承有两种:接口继承和实现继承。所以,Swift 和 Objective-C 都支持继承 :)
    猜你喜欢
    • 2019-04-14
    • 2014-12-27
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 2021-07-02
    • 2017-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多