Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在ViewController中将UIViewController修改为UICollectionViewController时出错?[快速]_Ios_Swift - Fatal编程技术网

Ios 在ViewController中将UIViewController修改为UICollectionViewController时出错?[快速]

Ios 在ViewController中将UIViewController修改为UICollectionViewController时出错?[快速],ios,swift,Ios,Swift,看来我缺乏swift的基础知识,所以我从一开始就在看YouTube youtube示例: 在视频的5:40~7:40秒时,我在class ViewController:UIViewController中得到一个错误,我将UIViewController更改为uicollectionviewcontroller, 但是有一个错误。视频中有一个错误。我看了看,并严格按照视频进行操作,但在视频中,错误被解决了,我没有。我认为代码是一样的,为什么错误没有得到解决 错误名称 import UIKit @

看来我缺乏swift的基础知识,所以我从一开始就在看YouTube

youtube示例

在视频的
5:40~7:40秒
时,我在
class ViewController:UIViewController
中得到一个错误,我将
UIViewController
更改为
uicollectionviewcontroller
, 但是有一个错误。视频中有一个错误。我看了看,并严格按照视频进行操作,但在视频中,错误被解决了,我没有。我认为代码是一样的,为什么错误没有得到解决

错误名称

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let layout = UICollectionViewFlowLayout()
        window?.rootViewController = ViewController(collectionViewLayout: layout)
        
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}
[UICollectionViewController loadView]实例化了脚本“Main”中标识符为“UIViewController-BYZ-38-t0r”的视图控制器,但未获取UICollectionView

我认为这是一个错误,因为我没有在情节提要上创建任何内容,但视频中的情节提要上没有任何内容

视图控制器

import UIKit

class ViewController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        collectionView.backgroundColor = .yellow
    }
}
AppDelegate

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let layout = UICollectionViewFlowLayout()
        window?.rootViewController = ViewController(collectionViewLayout: layout)
        
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}
导入UIKit
@UIApplicationMain
类AppDelegate:UIResponder、UIApplicationLegate{
变量窗口:UIWindow?
func应用程序(application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplication.launchOptions键:任意]?)->Bool{
let layout=UICollectionViewFlowLayout()
window?.rootViewController=ViewController(collectionViewLayout:layout)
返回真值
}
//MARK:UISceneSession生命周期
func应用程序(uApplication:UIApplication,用于连接的配置SceneSession:UISceneSession,选项:UIScene.ConnectionOptions)->UISceneConfiguration{
//在创建新场景会话时调用。
//使用此方法可以选择配置以创建新场景。
返回UISceneConfiguration(名称:“默认配置”,会话角色:ConnectionSceneSession.role)
}
func应用程序(application:UIApplication,didDiscardSceneSessions sceneSessions:Set){
//当用户放弃场景会话时调用。
//如果在应用程序未运行时丢弃了任何会话,则将在application:didFinishLaunchingWithOptions后不久调用该会话。
//使用此方法释放特定于被丢弃场景的任何资源,因为它们不会返回。
}
}
我努力搜索,但我不太明白,因为我缺乏关于swift的基本知识。
谢谢

您似乎正在使用Xcode 11+。视频现在有点过时了,因为它使用的是Xcode 10或更早版本。从Xcode 11开始,应用程序项目模板将为您创建SceneDelegate.swift。您应该改为在SceneDelegate中设置窗口的rootVC:

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?


    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let _ = (scene as? UIWindowScene) else { return }
        let layout = UICollectionViewFlowLayout()
        window?.rootViewController = ViewController(collectionViewLayout: layout)
    }
    ...

在这里,
window
变量将由UIKit分配给实际的
UIWindow
实例。另一方面,AppDelegate.swift中的
窗口
,您可能声明了自己(在Xcode 11之后,他们将
窗口
属性从AppDelegate移动到SceneDelegate),没有得到这种特殊处理。

检查脚本中是否有任何viewController?如果是,则删除它,然后添加新的UICollectionViewcontroller并为其分配ViewController类。您是否还有SceneDelegate.swift文件?谢谢Saurabh Prajapati还有一件事我很好奇,但在视频中,
ViewController
没有触及
情节提要中的任何内容,但他在启动视频之前创建了一个
uicollectionViewController
,并启动了它?是的,但在视频中没有任何东西可以触摸,因为没有使用SceneDelegate.swift文件。@je2请注意,视频中甚至没有SceneDelegate。录像带过时了。