kevin_b
kevin_b

Reputation: 853

Missing argument for parameter error in Swift

I am exploring Swift and trying to write a function that compares two Rank values by comparing their raw values. But I get the error: missing argument for parameter 'rawValue' in call card().compareValue(card.Ace, card.King)

Any ideas?

enum card : Int {
    case Ace = 1
    case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
    case King, Queen, Jack

    func cardValue() -> String {
        switch self {
        case .Ace:
            return "Ace"

        case .King:
            return "King"

        case .Queen:
            return "Queen"

        case .Jack:
            return "Jack"

        default:
            return String(self.rawValue)
        }
    }

    func compareValue(val1: card, val2: card) -> String {
        if val1.rawValue > val2.rawValue {
            return "\(val1) is greater than \(val2)"
        } else {
            return "\(val1) is not greater than \(val2)"
        }
    }
}

card().compareValue(card.Ace, card.King) // error

Upvotes: 0

Views: 3348

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

The problem is in the construction of the card() object, the target of the invocation:

card(). // and so on

You are trying to construct an enum without specifying its raw value. You can fix it by supplying it, like this

card(rawValue:0). // <<=== This is not a good fix!

but that is not a good fix. Instead, you should make compareValue a static or a free-standing function:

static func compareValue(val1: card, _ val2: card) -> String
//                                   ^
... // Note the underscore ----------+
card.compareValue(card.Ace, card.King) // Works

Upvotes: 2

Related Questions