xcode 实例成员不能用于类型 - 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52272817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Instance member cannot be used on type - error
提问by Hyman Bashford
I have been having a strange error, and have had no luck in trying to fix it. I've got some linked files, and I'm calling a function in one file (ViewController
) that's defined in another file (Sign.swift
). The function in Sign.swift
creates output defined in another file (GameState.swift
). I'm not sure what's wrong with this, I've done a lot of research, and even this answerdidn't help. (errors under code).
我遇到了一个奇怪的错误,并且没有尝试修复它。我有一些链接文件,我正在调用另一个文件 ( ViewController
) 中定义的文件 ( ) 中的函数Sign.swift
。函数 inSign.swift
创建在另一个文件 ( GameState.swift
) 中定义的输出。我不确定这有什么问题,我做了很多研究,即使这个答案也没有帮助。(代码下的错误)。
ViewController.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.
}
@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
:
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
:
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!"
}
}
}
I am getting errors in both files: Error 1 (commented line in ViewController.swift
) has the below error message:
我在两个文件中都遇到错误:错误 1(中的注释行ViewController.swift
)具有以下错误消息:
Instance member 'checkInputs` cannot be used on type 'Sign'; did you mean to use a value of this type instead?
实例成员“checkInputs”不能用于“Sign”类型;您的意思是使用这种类型的值吗?
Unfortunately I don't understand what I'm mean to fix from the above message, I'm new to Swift.
不幸的是,我不明白我想从上面的消息中解决什么问题,我是 Swift 的新手。
Error 2 (commented line in Sign.swift
) has this message:
错误 2(中的注释行Sign.swift
)有以下消息:
Cannot convert return expression of type 'GameState' to return type 'String'
无法将“GameState”类型的返回表达式转换为“String”类型
Again, I don't understand what's happening here. I don't have any errors in my GameState.swift
file though, so that means all errors should be in ViewController.swift
and Sign.swift
. How should I fix these errors?
同样,我不明白这里发生了什么。GameState.swift
不过,我的文件中没有任何错误,所以这意味着所有错误都应该在ViewController.swift
和 中Sign.swift
。我应该如何修复这些错误?
EDIT:I have updated my code, it now looks like this:
编辑:我已经更新了我的代码,现在看起来像这样:
ViewController.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
:
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
}
}
Note:New errors are on every line with the comment Error Emoji
, with this error message:
注意:新的错误出现在每一行带有注释的地方Error Emoji
,错误信息如下:
Instance member 'emoji' cannot be used on type 'Sign'
实例成员 'emoji' 不能用于类型 'Sign'
GameState.swift
: Unchanged.
GameState.swift
:不变。
I would appreciate all help with this.
我将不胜感激。
回答by Rakesha Shastri
If you need to use the method without an instance, you need to declare the method as static
.
如果需要在没有实例的情况下使用该方法,则需要将该方法声明为static
.
static func checkInputs(_ user: Sign, opponent: Sign) -> String
Note: You do not seem to using the user
that you pass. IMO you could skip asking for that parameter and use it as an instance method with playerChoice
.
注意:您似乎没有使用user
您通过的 。IMO 您可以跳过要求该参数并将其用作playerChoice
.
func checkInputs(opponent: Sign) -> String {
// Your logic
}
And then use it like this
然后像这样使用它
playerChoice.checkInputs(opponent: randomSign())
The second error is because you are trying to return an instance of Sign
instead of a String
. You need to either change the return type to Sign
or covert the Sign
in outcome
to String
- outcome.text
like @Larme pointed out?
第二个错误是因为您试图返回一个实例Sign
而不是一个String
. 您需要或是改变返回类型Sign
或隐蔽的Sign
在outcome
以String
-outcome.text
像@Larme指出?