iOS (swift) 中的多重继承和 AppDelegate

Multiple Inheritance and AppDelegate in iOS (swift)

我正在尝试在我的 iOS 应用程序 (swift 2) 中实现委托,但出现错误 "Multiple inheritance from classes 'UIViewController' and 'AppDelegate'." 我从空白创建了一个新的 MyAppDelegate swift 文件并得到相同的错误(但 'MyAppDelegate')。 MyAppDelegate.swift 的代码如下:

import Foundation
import MediaPlayer

class InstatunesAppDelegate {    
}

我在这里实现它并收到错误:

class myViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, InstatunesAppDelegate {

我仍在努力思考委托问题... MyAppDelegate 是如何成为超类的?如果我在 AppDelegate 中定义协议和委托,我如何在其他 类 中调用它们?任何帮助表示赞赏。提前致谢...

Swift 不支持多重 class 继承。您继承了一个 class 和其他协议。

不允许一次多重继承。如果你仔细检查 ViewController class 那么你会发现它实际上是从 UIViewController class 继承的,而其他(UITableViewDelegate,UITableViewDataSource)实际上是协议。这意味着 ViewController class 确认这些协议没有被继承。如果您想这样做,只需在 InstatunesAppDelegate 之前更改 "class" 并将其命名为 "protocol" ,如下所示:

protocol InstatunesAppDelegate{

}

为了更好地理解,请查看此 link protocol

原来我真正想要的是一个全球代表,在 AppDelegate.swift 中建立并且在我调用的任何 class 中可用:

let delegate = UIApplication.sharedApplication().delegate as! AppDelegate

感谢回复的人...