为什么我得到“Instance Member不能用于类型GameViewController”的错误?

时间:2016-08-29 17:56:12

标签: ios swift

标题是一个问题,另一个问题是如何将我创建的类连接到UILabel? 谁能帮帮我吗。如果你想知道我在SpriteKit做什么做文本冒险游戏。我不知道我是应该在spritekit还是单个视图应用程序中创建它。

这是我的代码:

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()

}

}

1 个答案:

答案 0 :(得分:1)

您收到该错误,因为在swift中,每个类都会收到自己的名称空间,并且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的子类是否只是稍后将获得更多实现的存根,但如果它们是Thing的简单重命名,则应使用{ {1}} 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' s currentThing都会设置locationcurrentThing设置为paper,因此locationName应该阅读"会议室。"

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