8

I found what looks like an elegant solution to iterating over enums here: How to enumerate an enum with String type?

Next, I'm having trouble figuring out how to call this method. At face value, it doesn't look like it takes an argument, but when I try to call Card.createDeck() I get a compiler error telling me "error: missing argument for parameter #1 in call".

Please let me know what I'm doing wrong here? What am I supposed to pass to this method?

struct Card {
    var rank: Rank
    var suit: Suit

    func simpleDescription() -> String {
        return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"
    }

    func createDeck() -> [Card] {

        var deck = [Card]()

        var n = 1
        while let rank = Rank.fromRaw(n) {

            var m = 1
            while let suit = Suit.fromRaw(m) {
                deck += Card(rank: rank, suit: suit)
                m++
            }
            n++
        }

        return deck
    }

}
Community
  • 1
  • 1
  • 1
    As said below, I figured out that because this is not an instance method, I need to use `static func`. However, when I call this now via `Card.createDeck()` I just see all the enums. How do I unwrap those? –  Jul 30 '14 at 20:35

2 Answers2

14

createDeck() is a instance method. Doing Card.createDeck() is a call to a class method that doesn't exist.

class func - for class methods

Edit:

I misread that it was a struct, but the same logic applies.

static func - for static methods

Literphor
  • 498
  • 4
  • 16
  • I just figured out from some other sleuthing that I need to use `static` before my `func`. This seems to be in line with your nudge, @literphor :-) –  Jul 30 '14 at 20:31
  • Oh yes, I didn't see it was a struct, otherwise you would use static. – Literphor Jul 30 '14 at 20:35
  • Thanks @literphor; next up, how to unwrap these enums that are flying at me? :-) –  Jul 30 '14 at 20:36
  • @davidjpeacock Either optional chaining `if let myEnum = myRank? { } else { } ` or unsafely force the unwrapping `let myEnum = myRank!` – Literphor Jul 30 '14 at 20:38
3

You can not able to call it directly as you need instace of struct as it is not class function.So use

Card(rank:Rank.yourRank,suit:Suit.yourSuit).createDeck()

Actually to make struct you need rank and suit instance so first make them and than pass to Card constructor.By default struct have arguments as their properties.

codester
  • 36,891
  • 10
  • 74
  • 72