1

I was doing an experiment of Swift programming book and stuck with construct a struct inner the struct itself. But the error reported the parameter is unwrapped. How could I take it value as parameter?

struct Card {
    var rank: Rank
    var suit: Suit
    func simpleDescription() -> String {
        return "The \(rank.simpleDescription()) of \(suit.simpleDescription())"
    }
    func FullDeck() -> Card[] {
        var deck: Card[]
        for i in 1...13
        {
            for j in 0...3
            {
                let rank_para = Rank.fromRaw(i)
                let suit_para = Suit.fromRaw(j)


                **deck.append(Card(rank: rank_para, suit : suit_para ))
                //value of optional type unwrapped;did you mean to use ? or !**

            }
        }
        return deck
    }
}




enum Rank: Int {
    case Ace = 1
    case Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten
    case Jack, Queen, King
    func simpleDescription() -> String {
        switch self {
        case .Ace:
            return "ace"
        case .Jack:
            return "jack"
        case .Queen:
            return "queen"
        case .King:
            return "king"
        default:
            return String(self.toRaw())
        }
    }
    func compare(sec:Rank) -> Bool {
        var first = 0
        var second = 0
        if self.toRaw() == 1 {
            first = 1
        } else {
            first = self.toRaw()
        }
        if sec.toRaw() == 1 {
            second = 1
        } else {
            second = self.toRaw()
        }
        return first > second
    }
}


enum Suit: Int{
    case Spades = 0
    case Hearts, Diamonds, Clubs
    func simpleDescription() -> String {
        switch self {
        case .Spades:
            return "spades"
        case .Hearts:
            return "hearts"
        case .Diamonds:
            return "diamonds"
        case .Clubs:
            return "clubs"
        }
    }
}

1 Answer 1

1

the fromRaw method returns an optional value: Rank? and Suit?. That means that the value could be nil. You need to check for that:

if let aRank = rank_para {
    if let aSuit = suit_para {
        deck.append(Card(rank: aRank, suit: aSuit))
    }
}

By using "if let", you "unwrap" the optional value into a value (aRank and aSuit) that is no longer optional (cannot be nil).

Another way to do that:

if rank_para and suit_para {
    deck.append(Card(rank: rank_para!, suit: suit_para!))
}

Here, you are checking if rank_para and suit_para are nil. If they both are not, you call append and "unwrap" the optional values using !. ! means if the value is nil throw a runtime error, otherwise, treat this variable as if it cannot be nil.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot for explaining the optional value and its unwrap for me. It works now. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.