Swift nested types

    xiaoxiao2021-03-26  30

    /*

       Nested types

       one type nexted in other type

     */

    struct BlackJackCard{

        enum Suit: String{

            case spades = "heitao",

            hearts="hongtao",diamonds="fangkuai",clubs="caohua"

        }

        enum Rank:Int{

            case two=2,three,four,five,six,seven,eight,nine,ten

            case jack,queen,king,ace

            struct Value{

                let first:Int,second:Int?

            }

            var values:Value{

                switch self {

                case .ace:

                    returnValue(first:1,second:11)

                case .jack,.queen,.king:

                    returnValue(first:10,second:nil)

                default:

                    returnValue(first:self.rawValue,second:nil)

                }

            }

        }

        

        let rank:Rank,suit:Suit

        var description:String{

            var output = "suit is \(suit.rawValue)"

            output += " value is \(rank.values.first)"

            if let second =rank.values.second{

                output += " or \(second)"

            }

            return output

        }

    }

    let theAceOfSpades = BlackJackCard(rank:.ace,suit:.spades)

    print("theAceOfSpades:\(theAceOfSpades.description)")

    enum Test:Int{

        case a=2,b,c,d,e

        case f,g,h

    }

    let heartsSymbol = BlackJackCard.Suit.hearts.rawValue

    var test=Test.f.rawValue

    转载请注明原文地址: https://ju.6miu.com/read-659058.html

    最新回复(0)