iOS Development--swift & Classic Syntax (22) Type nesting

Source: Internet
Author: User

Type nesting

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 type nesting, which supports nested types in enum types, classes, and struct bodies.

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.

type Nested instances

The following example defines a struct BlackjackCard ( 21 points ), which is used to simulate BlackjackCard the number of poker points in. BlackjackCarda struct contains 2 nested defined enumeration types Suit and Rank .

In a BlackjackCard rule, a Ace card can represent 1 or one, and Ace this feature of the card is represented by a struct nested in an enumerated type Rank Values .

struct Blackjackcard { Nested definition enum-type suit Enum Suit: Character { Case Spades = ♠, Hearts = "?", Diamonds = "?", Clubs = ♣ } Nested Definition enumeration Rank Enum Rank: Int { Case Both = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten Case Jack, Queen, King, Ace struct Values {  LetFirst: Int,Second: Int? } VarValues: Values { Switch Self { Case .Ace: Return Values(First: 1,Second: 11) Case .Jack, .Queen, .king: returnvalues (first: 10, Second:   default: return values (first: Self.toraw (), Second:nil)}}}//Blackjackcard properties and Methods let Rank:rank, Suit:suit var description:string {var output = "Suit is \ (Suit.toraw ())," Output + = "value is \ (Rank.values.first)" If let second = rank.values.second {output + = "or \ (second) "} Return output}}             

An enumeration of Suit four suits used to describe playing cards, with a Character value of one type representing the color symbol.

The enumeration type is used to describe the poker card from the ~,,,, Rank Ace 13 cards J Q K , and each with a Int type of value to represent the face values of the cards. (The Int value of this type does not apply,, Ace J Q , K the card).

As mentioned above, the enumerated type Rank defines a nested struct within itself Values . This structure contains two variables, only Ace two values, and the remaining cards have only one value. Valuestwo properties defined in a struct:

first, for Int second , for Int? , or "optional Int "

RankDefines a computed property that values initializes the instance with the appropriate value, based on the face value of the card, Values and assigns a value to it values . For J ,, the value of the type is used for the card with Q K Ace The special numeric value, for the numeric value Int .

BlackjackCardThe struct itself has two properties-and rank suit , similarly, defines a computed attribute, which is used to construct a description of the card's name and value, and to check for the existence of a description description rank suit second Second value, if any, with an optional type. The description of the second value is added to the original description.

Because BlackjackCard it is a struct without a custom constructor, it is known in memberwise initializers for Structure types that the struct has a default member constructor , so you can use the default initializerto initialize a new constant theAceOfSpades :

  let theaceofspades = blackjackcard (rank: ace, Suit:  . spades) println () //print out" Theaceofspades: suit  is♠, value is 1 or all "           

Although Rank Suit it is nested in BlackjackCard , it can still be referenced, so it can be referenced individually by the member names in the enumeration type when initializing the instance. In the example above, the description property can correctly output a pair of Ace cards with 1 and 112 values.

type-Nested references

The external reference to the 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 can make Suit , Rank and Values the names as short as possible, because their names will naturally be defined by the context of the definition.

iOS Development--swift & Classic Syntax (22) Type nesting

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.