iOS development Language Swift entry---nested types

Source: Internet
Author: User

iOS development Language Swift Starter Series-Nested types

Enumeration types are often used to implement the functionality of a particular class or struct. It is also possible to easily define a generic class or struct for use in environments with multiple variable types, and in order to do so, Swift allows you to define nested types, which can be defined in enum types, classes, and struct bodies that support nested types.
To nest another type within one type, the definition of a type that needs to be nested is written within the range {} of the nested type, and multilevel nesting can be defined as needed.
  

Nested type instances

The following example defines a struct Blackjackcard (21 points), which is used to simulate the poker points in the Blackjackcard. The Blackjackcard struct contains 2 nested definitions of enum type suit and Rank.
In the Blackjackcard rule, the Ace card can represent a feature of 1 or 11,ace that is represented by a struct that is nested in the enumeration rank.

structBlackjackcard {//nested definition enum-type suit    enumSuit:character { CaseSpades ="", Hearts ="", Diamonds ="", Clubs =""}//Nested Definition enumeration rank    enumRank:int { Caseboth =2, three, four, Five, Six, Seven, Eight, Nine, Ten CaseJack, Queen, King, Ace.structValues { LetFirst:int, Second:int? }varvalues:values {SwitchSelf { Case. Ace:returnValues (First:1, Second: One) Case. Jack,. Queen,. King:returnValues (First:Ten, Second:nil)default:returnValues (First:self.toRaw (), Second:nil)}}}//Blackjackcard properties and Methods     LetRank:rank, Suit:suitvardescription:string {varOutput ="Suit is \ (Suit.toraw ()),"Output + ="value is \ (Rank.values.first)"        if  LetSecond = rank.values.second {output + ="or \ (second)"}returnOutput}}

The

enumerated suit is used to describe the four suits of playing cards, and the values of a character type represent the color symbol. The enumeration type rank is used to describe cards from Ace ~10,j, Q, K, 13 cards, and to represent the face value of a card, respectively, with an int type.  (The value of this int type does not apply to Aces, J, Q, K's cards). As mentioned above, the enumeration rank defines a nested structure within itself as values. This struct contains two variables, only the ACE has two values, and the remaining cards have only one value. struct two properties defined in values: first is int second, int? , or "optional Int" Rank defines a calculated attribute values, which will initialize the values instance with the appropriate values based on the face value of the card, and assign the value to values.  For J, Q, K, Ace uses a special value, and a value of type int for a card with a numeric value. The BLACKJACKCARD structure itself has two properties-rank and suit, and also defines a computed attribute description, description attribute to use rank and suit content to build a description of the card name and value,  Use the optional type second to check if there is a second value, and if so, add a description of the second value in the original description. Because Blackjackcard is a struct without a custom constructor, you know in memberwise initializers for Structure types that the struct has a default member constructor, so you can use the default initializer To initialize the new constant theaceofspades:

.Ace.Spades)println("theAceOfSpades: \(theAceOfSpades.description)""theAceOfSpades: suit is , value is 1 or 11"

Although rank and suit are nested in Blackjackcard, they can still be referenced, so they can be referenced individually by the member names in the enumeration type when initializing the instance.  In the example above, the Description property correctly outputs 1 and 112 values for the ACE card. A reference to a nested type is externally referenced to a nested type, prefixed by the name of the nested type, plus the name of the property to be referenced:

let heartsSymbol = BlackjackCard.Suit.Hearts.toRaw""

For the above example, this allows the names of suit, Rank, and Values to be as short as possible, because their names will naturally be qualified by the defined context.

iOS development Language Swift entry---nested types

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.