gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 09:35:18 25 4
gpt4 key购买 nike

我遇到了一个奇怪的错误,但没有成功地修复它。我有一些链接文件,我在一个文件 (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 中的注释行)有以下错误消息:

Instance member 'checkInputs` cannot be used on type 'Sign'; did you mean to use a value of this type instead?

不幸的是,我不明白我要从上面的消息中修复什么,我是 Swift 的新手。

错误 2(Sign.swift 中的注释行)包含此消息:

Cannot convert return expression of type 'GameState' to return type 'String'

同样,我不明白这里发生了什么。我的 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 的每一行,错误消息如下:

Instance member 'emoji' cannot be used on type 'Sign'

GameState.swift:未更改

在此方面,我将不胜感激。

最佳答案

如果您需要在没有实例的情况下使用该方法,您需要将该方法声明为static

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

注意:您似乎没有使用您传递的 user。在我看来,您可以跳过询问该参数并将其用作 playerChoice 的实例方法。

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

然后像这样使用它

playerChoice.checkInputs(opponent: randomSign())

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

关于ios - 实例成员不能用于类型 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52272817/

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