Reputation: 44087
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 answer didn't help. (errors under code).
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!"
}
}
}
I am getting errors in both files: Error 1 (commented line in ViewController.swift
) has the below error message:
Instance member 'checkInputs` cannot be used on type 'Sign'; did you mean to use a value of this type instead?
Unfortunately I don't understand what I'm mean to fix from the above message, I'm new to Swift.
Error 2 (commented line in Sign.swift
) has this message:
Cannot convert return expression of type 'GameState' to return type '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?
EDIT: I have updated my code, it now looks like this:
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
}
}
Note: New errors are on every line with the comment Error Emoji
, with this error message:
Instance member 'emoji' cannot be used on type 'Sign'
GameState.swift
: Unchanged.
I would appreciate all help with this.
Upvotes: 2
Views: 24869
Reputation: 11243
If you need to use the method without an instance, you need to declare the method as 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
.
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?
Upvotes: 13