Swift Learning-22--Nested types

Source: Internet
Author: User

Enumerations are used to implement certain functions for a particular class or struct, and, similarly, enumerations can easily define a tool class or struct to be used for a complex type, and in order to do so, Swift allows you to define nested types that define nested, enumerated, class, and struct in the supported types.

To nest another type within a type, write the definition of a nested type within {} of its outer type, and define multilevel nesting as needed

struct blackjackcard{

Nested enumerations

Enum Suit:character {

Case spades = "?", Hearts = "?", Diampnds = "?", Clubs = "?"

}

Nested Rank Enumeration

Enum Rank:int {

Case 2, Three,four, Five, Six, Seven, Eight, Nine, Ten

Case Jack,queen,king,ace

struct Values {

Let First:int,second:int?

}

var values:values{

Switch Self {

Case. Ace:

Return Values.init (first:1, Second:11)

Case. Jack,. QUeen,. King:

Return Values.init (First:10, Second:nil)

Default

Return Values.init (First:self.rawValue, Second:nil)

}

}

}

Properties and methods of Blackjackcard

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

}

}

Referencing nested types

When a nested type is externally referenced, prefix the type name of the nested type with the type name of its outer type

Let Heartssymbol = BlackjackCard.Suit.Hearts.rawValue

Swift Learning-22--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.