【问题标题】:Has anyone seen this Generic class 'UIHostingController' requires that 'ContentView' conform to 'View'有没有人见过这个通用类'UIHostingController'要求'ContentView'符合'View'
【发布时间】:2020-10-11 15:50:06
【问题描述】:

得到一个编译时错误,指出:通用类 'UIHostingController' 要求 'ContentView' 符合 'View'

我尝试研究类型转换,但在我的搜索中没有发现任何有用的东西

XCODE 11.5 我的第一个 iOS 应用程序修女只看中一个视图和两个 Delegate 文件,我收到此错误,不明白为什么,您输入它的股票代码吗?如何 ?帮助 xcode 很烂 : (

场景代理:

// SceneDelegate.swift
// 播放大港4
//
// 由 dfds 于 2020 年 6 月 21 日创建。
// 版权所有 © 2020 dfds。版权所有。
//

导入 UIKit
导入 SwiftUI

类 SceneDelegate: UIResponder, UIWindowSceneDelegate {

    变量窗口:UIWindow?


    func场景(_场景:UIScene,willConnectTo会话:UISceneSession,选项connectionOptions:UIScene.ConnectionOptions){
        // 使用此方法可选择配置 UIWindow `window` 并将其附加到提供的 UIWindowScene `scene`。
        // 如果使用情节提要,`window` 属性将自动初始化并附加到场景中。
        // 此委托并不意味着连接场景或会话是新的(请参阅 `application:configurationForConnectingSceneSession`)。

        // 创建提供窗口内容的 SwiftUI 视图。
        让 contentView = ContentView()

        // 使用 UIHostingController 作为窗口根视图控制器。
        如果让 windowScene = 场景为? UIWindowScene {
            让 window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: contentView)
            self.window = 窗口
            window.makeKeyAndVisible()
        }
    }

    func sceneDidDisconnect(_ 场景: UIScene) {
        // 当场景被系统释放时调用。
        // 这发生在场景进入后台后不久,或者它的会话被丢弃时。
        // 释放与该场景关联的任何资源,这些资源可以在下次场景连接时重新创建。
        // 场景可能稍后重新连接,因为它的会话没有必要被丢弃(请参阅 `application:didDiscardSceneSessions`)。
    }

    func sceneDidBecomeActive(_ 场景:UIScene){
        // 当场景从非活动状态移动到活动状态时调用。
        // 使用此方法重新启动在场景不活动时暂停(或尚未启动)的任何任务。
    }

    func sceneWillResignActive(_ scene: UIScene) {
        // 当场景从活动状态移动到非活动状态时调用。
        // 这可能是由于临时中断(例如来电)而发生的。
    }

    func sceneWillEnterForeground(_ scene: UIScene) {
        // 当场景从背景过渡到前景时调用。
        // 使用此方法撤消进入后台时所做的更改。
    }

    func sceneDidEnterBackground(_ 场景: UIScene) {
        // 当场景从前景过渡到背景时调用。
        // 使用该方法保存数据,释放共享资源,存储足够的场景特定状态信息
        // 将场景恢复到当前状态。
    }


}

内容视图:

import MediaPlayer


class ContentView: UIViewController {

    var musicPlayer = MPMusicPlayerController.applicationMusicPlayer
    
    override func viewDidLoad() {
        super.viewDidLoad()

  
    }
    
    @IBAction func stopButtonTapped(_ sender: UIButton) {
        musicPlayer.stop()
    }
    @IBAction func nextButtonTapped(_ sender: UIButton) {
        musicPlayer.skipToNextItem()
    }
    @IBAction func previousButtonTapped(_ sender: UIButton) {
        musicPlayer.skipToPreviousItem()
    }
    @IBAction func musicButtonTapped(_ sender: UIButton) {
        
        MPMediaLibrary.requestAuthorization {(status) in
            if status == .authorized{
                self.playArtist(artist: sender.currentTitle!)
            }
        }
        
        playArtist(artist: sender.currentTitle!)
    }
    
    func playArtist (artist: String){
        
        musicPlayer.stop();
        
        let query = MPMediaQuery()
        let predicate = MPMediaPropertyPredicate(value: artist, forProperty: MPMediaItemPropertyArtist)
        query.addFilterPredicate(predicate)
        musicPlayer.setQueue(with: query)
        musicPlayer.shuffleMode = .songs
        musicPlayer.play()
    }
    
}


【问题讨论】:

    标签: ios swift xcode11


    【解决方案1】:

    您正在尝试将继承自 UIViewController 的类设置为 rootViewUIHostingControllerrootViewUIHostingController 始终必须符合 View 协议。如果您尝试将UIViewController 用作rootViewController,则直接在SceneDelegate 中设置它。解决您的问题

    替换这个:

    window.rootViewController = UIHostingController(rootView: contentView)
    

    有了这个:

    window.rootViewController = contentView
    

    注意:命名声明时最好遵循标准,更改ContentView -> ContentViewController

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 2010-10-29
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多