Swift:使用泛型将协议的多个实现作为参数

Swift:使用泛型将协议的多个实现作为参数,swift,class,generics,abstract,swift-protocols,Swift,Class,Generics,Abstract,Swift Protocols,我的目标是接受协议的所有实现作为Swift中的参数。然而,当我尝试这样做时,我得到一个错误,说: 协议“KHKContent”只能用作泛型约束,因为它具有自身或关联的类型要求 我认为解决这个问题的最好方法是泛型,尽管我不确定。我是泛型的初学者,所以我很难理解我做错了什么 这是我的密码: protocol KHKContent: Identifiable, Equatable { var url: URL { get } var shareUrl: URL { get } var title:

我的目标是接受协议的所有实现作为Swift中的参数。然而,当我尝试这样做时,我得到一个错误,说:

协议“KHKContent”只能用作泛型约束,因为它具有自身或关联的类型要求

我认为解决这个问题的最好方法是泛型,尽管我不确定。我是泛型的初学者,所以我很难理解我做错了什么

这是我的密码:

protocol KHKContent: Identifiable, Equatable {
var url: URL { get }
var shareUrl: URL { get }

var title: Title? { get }
var author: Author? { get }
var authorType: AuthorType? { get }
var description: Description? { get }

var thumbnail: UIImage? { get }
var thumbnailUrl: URL? { get }

var uploadDate: Date? { get }
var categories: [Category]? { get }

init(url: URL, title: Title, author: Author, authorType: AuthorType, description: Description, uploadDate: Date?, categories: [Category])

init?(url urlString: String, title: Title, author: Author, authorType: AuthorType, description: Description, uploadDate: Date?, categories: [Category])

init?(url: URL)

init?(url urlString: String)

static func clean(_ str: String) -> String

static func getMetaDataFrom(url: URL, completion: (_ title: Title, _ author: Author, _ authorType: AuthorType, _ description: Description, _ categories: [Category], _ uploadDate: Date) -> Void)
}
初始值设定项是隐藏的,因为它们很长&可能不重要:

class KHKAudio: KHKContent {
var url: URL
var shareUrl: URL {
    print("shareURL not implemented.")
    return URL(string: "https://www.google.com")!
}

var title: Title?
var author: Author?
var authorType: AuthorType?
var description: Description?

var thumbnail: UIImage? = nil
var thumbnailUrl: URL? = nil

var uploadDate: Date?
var categories: [Category]?
}

class KHKVideo: KHKContent {
var url: URL
var shareUrl: URL {
    print("shareURL not implemented.")
    return URL(string: "https://www.google.com")!
}

var thumbnailUrl: URL?
var thumbnail: UIImage?

var title: Title?
var author: Author?
var authorType: AuthorType?
var description: Description?

var uploadDate: Date?
var categories: [Category]?
}
问题是,每当我想定义一个类时,我都会这样做:

class HomeViewModel<T: KHKContent>: ContentViewModel {
@Published var content: [T]?
}
类HomeViewModel:ContentViewModel{
@已发布的var内容:[T]?
}
但每当我想定义那个类时,我都会对那个类和实例化那个类的类做同样的事情,依此类推。它可以一直追溯到根视图

struct RootView<T: KHKContent>: View {
@ObservedObject var model: RootViewModel = RootViewModel<T>()
}
struct RootView:View{
@ObservedObject变量模型:RootViewModel=RootViewModel()
}
…可追溯到SceneDelegate

class SceneDelegate<T: KHKContent>: UIResponder, UIWindowSceneDelegate {

var window: UIWindow?

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
    // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
    // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
    // Create the SwiftUI view that provides the window contents.
    let contentView = RootView<T>()
...
class SceneDelegate:UIResponder,UIWindowSceneDelegate{
变量窗口:UIWindow?
func场景(场景:UIScene,willConnectTo会话:UISceneSession,选项connectionOptions:UIScene.connectionOptions){
//使用此方法可以选择性地配置UIWindow`window`并将其附加到提供的UIWindowScene`scene`。
//如果使用情节提要,“窗口”属性将自动初始化并附加到场景。
//此委托并不意味着连接的场景或会话是新的(请参见'application:ConfigurationForConnectionSceneSession'。
//创建提供窗口内容的SwiftUI视图。
让contentView=RootView()
...
然后项目编译并运行,但我得到一个黑屏和以下消息:

UIWindowsCEnsessionRoleApplication的Info.plist配置“默认配置”包含UISceneDelegateClassName键,但无法加载名为“******.SceneDelegate”的类


如果您能帮助我,我将不胜感激。非常感谢。

如果您从ScenedLegate中删除泛型,它是否有效?使用普通视图?如果是,我只需将所有内容向下移动一个视图,这样ScenedLegate就不必知道泛型。另外,在一个侧面,不要使用ObservedObject。如何使用StateObject我可以这样做吗?如果我将所有视图向下移动到一个视图中,如果不在该视图中写入,就无法定义下一个视图。如果没有泛型,ScenedLegate是否可以工作?是否可以使用普通的RootView启动项目?否。为了实例化RootView,我需要编写RootView()。请尝试普通的RootView,不要使用泛型。由于您没有提供最小的可复制示例,我正在尝试查看您是否正确设置了SceneDelegate。删除所有泛型的提及,只需查看项目是否有效。对于不熟悉设置SceneDelegate的人