实例成员不能用于类型-错误

时间:2018-09-11 09:27:18

标签: ios swift xcode enums

我遇到了一个奇怪的错误,也没有运气去修复它。我有一些链接文件,我正在一个文件(ViewController)中定义的一个文件(Sign.swift)中调用函数。 Sign.swift中的函数创建在另一个文件(GameState.swift)中定义的输出。我不确定这有什么问题,我已经做了很多研究,甚至this answer也没有帮助。 (代码错误)。

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet weak var computerLabel: UILabel!
    @IBOutlet weak var output: UILabel!
    @IBOutlet weak var rockButton: UIButton!
    @IBOutlet weak var paperButton: UIButton!
    @IBOutlet weak var scissorsButton: UIButton!
    @IBOutlet weak var playAgain: UIButton!

    @IBAction func rockButtonPressed(_ sender: UIButton) {
        computerLabel.text = randomSign()
        var playerChoice = Sign.rock
        var output = Sign.checkInputs(playerChoice, computerLabel.text) //Error 1 is here
    }

    @IBAction func paperButtonPressed(_ sender: UIButton) {

    }

    @IBAction func scissorsButtonPressed(_ sender: UIButton) {

    }

    @IBAction func playAgainPressed(_ sender: UIButton) {

    }


}

Sign.swift

import Foundation
import GameplayKit

let randomChoice = GKRandomDistribution(lowestValue: 0, highestValue: 2)

func randomSign() -> String {
    let sign = randomChoice.nextInt()
    if sign == 0 {
        return ""
    }
    else if sign == 1 {
        return ""
    }
    else {
        return "✌️"
    }
}

enum Sign {
    case rock, paper, scissors

    var emoji: String {

        switch self {

        case .rock:
            return ""
        case.paper:
            return ""
        case.scissors:
            return "✌️"
        }
    }

    func checkInputs(_ user: Sign, opponent: Sign) -> String {
        var outcome = GameState.draw
        if (opponent == .rock) {
            if (emoji == "") {
                outcome = .draw
            }
            else if (emoji == "") {
                outcome = .win
            }
            else {
                outcome = .lose
            }
        }
        else if (opponent == .paper) {
            if (emoji == "") {
                outcome = .lose
            }
            else if (emoji == "") {
                outcome = .draw
            }
            else {
                outcome = .win
            }
        }
        else if (opponent == .scissors) {
            if (emoji == "") {
                outcome = .win
            }
            else if (emoji == "") {
                outcome = .lose
            }
            else {
                outcome = .draw
            }
        }

        return outcome //Error 2 is here
    }

}

GameState.swift

import Foundation

enum GameState {
    case start, win, lose, draw

    var text: String {

        switch self {
        case.start:
            return "Game started!"

        case.win:
            return "You win!"

        case.lose:
            return "You lose!"

        case.draw:
            return"It's a draw!"
        }

    }

}

两个文件都出现错误:错误1(ViewController.swift中的注释行)显示以下错误消息:

  

实例成员'checkInputs'不能用于'Sign'类型;你是否   是要改用这种类型的值?

不幸的是,我不明白上述消息是什么意思,我是Swift的新手。

错误2(Sign.swift中的注释行)显示以下消息:

  

无法将类型'GameState'的返回表达式转换为返回类型   “字符串”

再次,我不明白这里发生了什么。但是我的GameState.swift文件中没有任何错误,因此这意味着所有错误都应该在ViewController.swiftSign.swift中。我该如何解决这些错误?

编辑:我已经更新了代码,现在看起来像这样:

ViewController.swift

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func computerChoose() {

    }

    @IBOutlet weak var computerLabel: UILabel!
    @IBOutlet weak var output: UILabel!
    @IBOutlet weak var rockButton: UIButton!
    @IBOutlet weak var paperButton: UIButton!
    @IBOutlet weak var scissorsButton: UIButton!
    @IBOutlet weak var playAgain: UIButton!

    @IBAction func rockButtonPressed(_ sender: UIButton) {
        var random = randomSign()
        var stringRandom = ""
        switch random {
        case .rock :
            stringRandom = ""
        case .paper:
            stringRandom = ""
        case .scissors:
            stringRandom = "✌️"
        }
        var playerChoice = Sign.rock
        var output = Sign.checkInputs(random)
        computerLabel.text = stringRandom
    }

    @IBAction func paperButtonPressed(_ sender: UIButton) {

    }

    @IBAction func scissorsButtonPressed(_ sender: UIButton) {

    }

    @IBAction func playAgainPressed(_ sender: UIButton) {

    }


}

Sign.swift

import Foundation
import GameplayKit

let randomChoice = GKRandomDistribution(lowestValue: 0, highestValue: 2)

func randomSign() -> Sign {
    let sign = randomChoice.nextInt()
    if sign == 0 {
        return .rock
    }
    else if sign == 1 {
        return .paper
    }
    else {
        return .scissors
    }
}

enum Sign {
    case rock, paper, scissors

    var emoji: String {

        switch self {

        case .rock:
            return ""
        case.paper:
            return ""
        case.scissors:
            return "✌️"
        }
    }

    static func checkInputs(_ opponent: Sign) -> GameState {
        var outcome = GameState.draw
        if (opponent == .rock) {
            if (emoji == "") { //Error Emoji
                outcome = .draw
            }
            else if (emoji == "") { //Error Emoji
                outcome = .win
            }
            else {
                outcome = .lose
            }
        }
        else if (opponent == .paper) { //Error Emoji
            if (emoji == "") {
                outcome = .lose
            }
            else if (emoji == "") { //Error Emoji
                outcome = .draw
            }
            else {
                outcome = .win
            }
        }
        else if (opponent == .scissors) {
            if (emoji == "") { //Error Emoji
                outcome = .win
            }
            else if (emoji == "") { //Error Emoji
                outcome = .lose
            }
            else {
                outcome = .draw
            }
        }

        return outcome
    }

}

注意:每行带有注释Error Emoji的新错误,并显示以下错误消息:

  

实例成员'emoji'不能用于'Sign'类型

GameState.swift未更改

在此我将不胜感激。

1 个答案:

答案 0 :(得分:6)

如果您需要使用没有 instance 的方法,则需要将该方法声明为static

static func checkInputs(_ user: Sign, opponent: Sign) -> String

注意:您似乎并没有使用通过的user。 IMO,您可以跳过询问该参数的过程,而将其用作playerChoice的实例方法。

func checkInputs(opponent: Sign) -> String {
    // Your logic
}

然后像这样使用它

playerChoice.checkInputs(opponent: randomSign())

第二个错误是因为您试图返回Sign而不是String的实例。您需要像@Larme所指出的那样,将返回类型更改为Sign还是将Sign中的outcome转换为String-outcome.text