简体   繁体   English

Swift:无法在属性初始化程序中使用实例成员“trialGame”; 属性初始化程序在“自我”可用之前运行

[英]Swift: Cannot use instance member 'trialGame' within property initializer; property initializers run before 'self' is available

In my Swift ViewController, I have the code在我的 Swift ViewController 中,我有代码

class ViewController: UIViewController {
    
    var camera: UIButton!
    
    var gameBrowser: UICollectionView!
    let gameBrowserReuseID = "gamecell"
    
    var games: [Game]!
    
    
    let trialGame = Game(id: "5", title: "Dog", release_date: "1989", publisher: "Nintendo", price: "20", platform: "OS X", category: "Adventure", players: ["A", "B"])
    let trialGame2 = Game(id: "5", title: "Dog", release_date: "1989", publisher: "Nintendo", price: "20", platform: "OS X", category: "Adventure", players: ["A", "B"])
    
    games = [trialGame, trialGame2]

However, on the last line, I'm getting the error "Cannot use instance member 'trialGame' within property initializer; property initializers run before 'self' is available" (and a second one for trialGame2).但是,在最后一行,我收到错误“不能在属性初始化程序中使用实例成员 'trialGame';属性初始化程序在 'self' 可用之前运行”(以及 trialGame2 的第二个)。 I searched this error on other forums, and it seems like this error is generally caused because one or more variables in the erroneous line of code are not available at the same time, but I'm not sure why this would be the case, because all I'm doing is declaring two objects of class Game as constants.我在其他论坛上搜索了这个错误,似乎这个错误一般是因为错误代码行中的一个或多个变量不能同时使用,但我不确定为什么会出现这种情况,因为我所做的只是将 class Game 的两个对象声明为常量。 Since they're both declared before I initialize array games, why aren't they both available?由于它们都是在我初始化数组游戏之前声明的,为什么它们都不可用?

Most posts recommend to use the keyword 'lazy' in front of the erroneous line of code, so: lazy var games = [trialGame, trialGame2] but for some reason the compiler is telling me that I need an initialization for keyword lazy.大多数帖子建议在错误的代码行前使用关键字“lazy”,因此: lazy var games = [trialGame, trialGame2]但由于某种原因,编译器告诉我我需要对关键字lazy 进行初始化。 I'm not sure what this means, and the compiler's only recommendation is to remove the keyword.我不确定这意味着什么,编译器的唯一建议是删除关键字。

If it helps, here is my Game class:如果有帮助,这是我的游戏 class:

import UIKit

class Game: Codable {
    var id: String!
    var title: String!
    var release_date: String!
    var publisher: String!
    var price: String!
    var platform: String!
    
    var category: String!
    var players: [String]!
    
    init(id: String, title: String, release_date: String, publisher: String, price: String, platform: String, category: String, players: [String]){
        self.id = id
        self.title = title
        self.release_date = release_date
        self.publisher = publisher
        self.price = price
        self.platform = platform
        self.category = category
        self.players = players
    }
}

I'm feeling very lost here, since both the reason behind the error and the solution are eluding me.我在这里感到非常迷茫,因为错误背后的原因和解决方案都在逃避我。 Grateful for any help!感谢您的帮助!

First of all never, never ever declare properties as implicit unwrapped optionals which are initialized with non-optional values.首先,永远,永远不要将属性声明为使用非可选值初始化的隐式未包装选项。 Remove all exclamation marks!去掉所有感叹号!

To answer your question you cannot run arbitrary code outside of a method in a class, why not simply要回答您的问题,您不能在 class 中的方法之外运行任意代码,为什么不简单地

class ViewController: UIViewController {
    
    var camera: UIButton!
    
    var gameBrowser: UICollectionView!
    let gameBrowserReuseID = "gamecell"
    
    var games = [Game(id: "5", title: "Dog", release_date: "1989", publisher: "Nintendo", price: "20", platform: "OS X", category: "Adventure", players: ["A", "B"]),
                 Game(id: "5", title: "Dog", release_date: "1989", publisher: "Nintendo", price: "20", platform: "OS X", category: "Adventure", players: ["A", "B"])]
    
  

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 属性包装器:“不能在属性初始值设定项中使用实例成员;属性初始值设定项在 'self' 可用之前运行” - Property Wrappers: "Cannot use instance member within property initializer; property initializers run before 'self' is available" (MVVM) 不能在属性初始化器中使用实例成员“模型”; 属性初始化程序在“自我”可用之前运行 - (MVVM) Cannot use instance member 'model' within property initializer; property initializers run before 'self' is available 不能在属性初始化程序中使用实例成员“pdfName”; 属性初始化程序在“自我”可用之前运行 - Cannot use instance member 'pdfName' within property initializer; property initializers run before 'self' is available SwidtUI iOS - 无法在属性初始值设定项中使用实例成员 x; 属性初始值设定项在“self”可用之前运行 - SwidtUI iOS - Cannot use instance member x within property initializer; property initializers run before 'self' is available Swift无法在属性初始化程序中使用实例成员 - Swift Cannot use instance member within property initializer 无法在属性初始化程序中使用实例成员 - Cannot use instance member within property initializer 无法在属性初始值设定项中使用实例成员 - Cannot use instance member within property initializer 属性初始值设定项在“自我”可用之前运行错误? - Property initializers run before 'self' is available Error? 无法在属性初始化程序中使用实例成员“ parseData” - Cannot use instance member 'parseData' within property initializer 无法在DataController Init的属性初始化器错误中使用实例成员 - Cannot use instance member within property initializer error in DataController Init
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM