gpt4 book ai didi

ios - 为什么我会收到 "Instance Member cannot be used on type GameViewController"错误?

转载 作者:行者123 更新时间:2023-11-28 06:35:24 26 4
gpt4 key购买 nike

标题是一个问题,与该问题相关的另一个问题是如何将我创建的类连接到 UILabel?谁能帮帮我吗。如果您想知道我在做什么,那就是在 SpriteKit 中制作文本冒险游戏。我不知道我是否应该在 spritekit 或单 View 应用程序中制作它。

这是我的代码:

import UIKit
import SpriteKit

class GameViewController: UIViewController {

@IBOutlet weak var locationName: UILabel! // Name of the Location of the game
@IBOutlet weak var userInputArrows: UILabel! // Arrows for user input
@IBOutlet weak var objectiveName: UILabel! // Name of the objective of the game
@IBOutlet weak var computerOutput: UILabel! // The AI's output or return text
@IBOutlet weak var userInputText: UITextField! // Humans input text

class Thing {
var location: String
var name: String
var computerDescription: String
init(location: String, name: String, computerDescription: String) {
self.location = location
self.name = name
self.computerDescription = computerDescription

let location = locationName.text
}
}
// Main Location is Abandoned Power Plant
// Start Location: Inside a dim lit room

let roomAbandonedPowerPlant = Thing(location: "Room", name: "ROOM", computerDescription: "A small dimly lit room " +
"with large heavy machinery. With almost complete silence.")

let machineryAbandonedPowerPlant = Thing(location: "Room", name: "MACHINERY", computerDescription: "Old corroded " +
"machines, looks like it hasn't been used in a long time.")

let paperAbandonedPowerPlant = Thing(location: "Room", name: "PAPER", computerDescription: "All crumbled up with " +
"dirt covering everything, and the lettering fading away.")

let writingAbandonedPowerPlant = Thing(location: "Room", name: "Paper WRITING", computerDescription: "The paper says: " +
"The only way out of this room is to use your imagination.")

let machineryLetteringPowerPlant = Thing(location: "Room", name: "MACHINE LETTERING", computerDescription: "It says:" +
"Built - 1890, Occupation - Used for spare oil, Made By - John Mac")

let firstKeychainPowerPlant = Thing(location: "Room", name: "KEYCHAIN", computerDescription: "Slightly shining in the dim" +
" sunlight.")

let keychainPowerPlant = Thing(location: "Room", name: "VIEW KEYCHAIN", computerDescription: "It say's: Owner: John Mac")


class Room: Thing {
}
let room = Room(location: "Room", name: "Room", computerDescription: "A small dimly lit room with large heavy " +
"machinery. With almost complete silence.")

class Machinery: Thing {
}
let machinery = Machinery(location: "Room", name: "Machinery", computerDescription: "Old corroded machines, looks " +
"like it hasn't been used in a long time.")

class Paper: Thing {
}
let paper = Paper(location: "Room", name: "Paper", computerDescription: "All crumbled up with dirt covering " +
"everything, and the lettering fading away.")

class PaperWriting: Thing {
}
let paperwriting = PaperWriting(location: "Room", name: "Paper Writing", computerDescription: "The Paper says: " +
"The only way out of this room is to use your imagination.")

class MachineLettering: Thing {
}
let machinelettering = MachineLettering(location: "Room", name: "Machine Lettering", computerDescription: "It says: " +
"Built - 1890, Occupation - Used fir spare oil, Made By - John Mac")

class Keychain: Thing {
}
let keychain = Keychain(location: "Room", name: "Keychain", computerDescription: "Slightly shining in the dim sunlight.")

class ViewKeychain: Thing {
}
let viewkeychain = ViewKeychain(location: "Room", name: "View Keychain", computerDescription: "It says: Owner: John " +
"Mac")

override func viewDidLoad() {
super.viewDidLoad()

}

最佳答案

您收到该错误是因为在 swift 中每个类都有自己的 namespace 并且 locationName 未在 Thing 中定义。您更大的问题是 encapsulation 之一.虽然可以有像这样的嵌套类,但这种情况并不常见。相反,类或对象应该是具有自己的属性和操作这些属性的方法的离散实体。

我会将 Thing 及其子类移到 GameViewController 之外。 (或者更好的是,将其移动到自己的文件中)。例如:

class Thing {
var location: String
var name: String
var computerDescription: String

init(location: String, name: String, computerDescription: String)
{
self.location = location
self.name = name
self.computerDescription = computerDescription
}
}

class Machinery: Thing {
}

class Paper: Thing {
}

class PaperWriting: Thing {
}

class MachineLettering: Thing {
}

class Keychain: Thing {
}

class ViewKeychain: Thing {
}

class Room: Thing {}

class GameViewController: UIViewController
{

@IBOutlet weak var locationName: UILabel! // Name of the Location of the game
@IBOutlet weak var userInputArrows: UILabel! // Arrows for user input
@IBOutlet weak var objectiveName: UILabel! // Name of the objective of the game
@IBOutlet weak var computerOutput: UILabel! // The AI's output or return text
@IBOutlet weak var userInputText: UITextField! // Humans input text

// Main Location is Abandoned Power Plant
// Start Location: Inside a dim lit room

let roomAbandonedPowerPlant = Thing(location: "Room", name: "ROOM", computerDescription: "A small dimly lit room " +
"with large heavy machinery. With almost complete silence.")

let machineryAbandonedPowerPlant = Thing(location: "Room", name: "MACHINERY", computerDescription: "Old corroded " +
"machines, looks like it hasn't been used in a long time.")

let paperAbandonedPowerPlant = Thing(location: "Room", name: "PAPER", computerDescription: "All crumbled up with " +
"dirt covering everything, and the lettering fading away.")

let writingAbandonedPowerPlant = Thing(location: "Room", name: "Paper WRITING", computerDescription: "The paper says: " +
"The only way out of this room is to use your imagination.")

let machineryLetteringPowerPlant = Thing(location: "Room", name: "MACHINE LETTERING", computerDescription: "It says:" +
"Built - 1890, Occupation - Used for spare oil, Made By - John Mac")

let firstKeychainPowerPlant = Thing(location: "Room", name: "KEYCHAIN", computerDescription: "Slightly shining in the dim" +
" sunlight.")

let keychainPowerPlant = Thing(location: "Room", name: "VIEW KEYCHAIN", computerDescription: "It say's: Owner: John Mac")

let room = Room(location: "Room", name: "Room", computerDescription: "A small dimly lit room with large heavy " +
"machinery. With almost complete silence.")

let machinery = Machinery(location: "Room", name: "Machinery", computerDescription: "Old corroded machines, looks " +
"like it hasn't been used in a long time.")

let paper = Paper(location: "Room", name: "Paper", computerDescription: "All crumbled up with dirt covering " +
"everything, and the lettering fading away.")


let paperwriting = PaperWriting(location: "Room", name: "Paper Writing", computerDescription: "The Paper says: " +
"The only way out of this room is to use your imagination.")

let machinelettering = MachineLettering(location: "Room", name: "Machine Lettering", computerDescription: "It says: " +
"Built - 1890, Occupation - Used fir spare oil, Made By - John Mac")

let keychain = Keychain(location: "Room", name: "Keychain", computerDescription: "Slightly shining in the dim sunlight.")

let viewkeychain = ViewKeychain(location: "Room", name: "View Keychain", computerDescription: "It says: Owner: John " +
"Mac")

var currentThing: Thing?
{
didSet
{
self.locationName.text = self.currentThing?.location
}
}

override func viewDidLoad() {
super.viewDidLoad()
self.currentThing = self.paper

}

}

此外,还不清楚 Thing 的子类是否只是 stub ,以后会得到更多的实现,但如果它们只是简单地重命名 Thing,你应该使用 typealias typealias Room = Thing 而不是 class Room: Thing {}

我对您将类连接到 UILabel 的意思感到困惑。但是您可以实现一个属性观察器,它会在每次设置属性时更新标签,例如:

var currentThing: Thing?
{
didSet
{
self.locationName.text = self.currentThing?.location
}
}

override func viewDidLoad() {
super.viewDidLoad()
self.currentThing = self.paper

}

在这里,属性 currentThing 被声明为可选的 Thing。每次设置 currentThing 时,locationName 的文本都会设置 currentThinglocationcurrentThing 设置为 paper,因此 locationName 应显示为“Room”。

到目前为止,您只使用了 UIKit,因此 SpriteKit 应用程序没有意义。基于单一 View 应用程序模板的应用程序会更有意义。

关于ios - 为什么我会收到 "Instance Member cannot be used on type GameViewController"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39212306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com