[Swift] Type components ~ Type nesting ~

What is type nesting?

In Swift, you can define a type inside a type, which is called ** type nesting **.

Nested types will inherit the name of the nested type, so ** The advantage is that the type name can be made clearer and more concise. ** **

Definition method

To nest types, define the type inside the type definition.

As an example, suppose you have the following code.

Sports type that shows the status of the person who was playing sports, Suppose there is a SportsKind type that represents the type of sport.


enum SportsKind {
    case tennis
    case soccer
    case baseball
}

struct Sports {
    var name: String
    var history: Int
    let kind: SportsKind
}

In this case, it can be inferred that the SportsKind type represents the type of Sports type, It's just tied up by naming.

Like the following sample code If you nest the SportsKind type inside the Sports type and rename it to Kind, It will be Sports.Kind type.

** Sports.Kintd type is compared to SportsKind type The relationship with the Sports type is clear. ** **

Also, as you can see from the let kind: Kind, Because Sports.Kind type can be referred as Kind type inside Sports type The type name can now be described more concisely.


struct Sports {
    enum Kind {
        case tennis
        case soccer
        case baseball
    }
    
    var name: String
    var history: Int
    let kind: Kind   
    
    init(name: String, kind: Kind, history: Int) {
        self.name = name
        self.kind = kind
        self.history = history
    }
    
    func status() {
        // "\n"Represents a line break.
        print("name:\(name)\n sports:\(kind)\n Experience:\(history)Year")
    }
}

let humanAKind = Sports.Kind.baseball
let humanBKind = Sports.Kind.soccer
let humanA = Sports(name: "Sakamoto", kind: humanAKind, history: 30)
let humanB = Sports(name: "Honda", kind: humanBKind, history: 25)

humanA.status()
print("---")
humanB.status()

Execution result
Name: Sakamoto
Sports: baseball
Experience: 30 years
---
Name: Honda
Sports: soccer
Experience: 25 years

That concludes our discussion of type nesting!

The volume of the article that just puts the mold in the mold is small, Type nesting is quite important.

As an aside, ** String type and Int type also have more types in the type. ** **

Because it is a function that is commonly used in so many places Please be able to use it!

There are other articles that describe the components of the type, so be sure to check them out as well.

-[Swift] Type component-Type basics- -[Swift] type component-property first part-[Swift] type component ~ initializer ~ -[Swift] type component ~ method ~ -[Swift] type component ~ subscript ~ -[Swift] type component ~ extension ~

Thank you for watching until the end.

Recommended Posts

[Swift] Type components ~ Type nesting ~
[Swift] Type type ~ Enumeration type ~
[Swift] Type type ~ Structure ~
[Swift] Type component ~ Subscript ~
[Swift] Type design guidelines
[Swift] Type component ~ Initializer ~
[Swift] Shared enumeration type
[Swift] Type type-Class sequel-
[Swift] Type component ~ Method ~
[Swift] Summary about Bool type
[Swift] Type type-Class first part-
[Swift] Type component ~ Property Part 2 ~
Great Swift pointer type commentary
[Swift] Converts a UInt64 type integer to [UInt8]