Type 1 Check
1. type check Operators
Type check is a way to check or convert an instance type to another type.
In Swift, type checks are implemented using the is and as operators.
The is operator is used to check whether an instance is of a specific type. If yes, true is returned. Otherwise, false is returned.
The as operator is used to transform an instance to another type. Because the instance transformation may fail, Swift provides two forms for the as operator: Option form? And mandatory form.
Option format (?) And return an option value of the expected type. If the conversion is successful, the returned options include valid values; otherwise, the option value is nil.
The forced form (as) operation executes the forced conversion from an instance to the target type. Therefore, using this form may trigger a runtime error.
As shown in the following example.
Class MediaItem {
Var name: String
Init (name: String ){
Self. name = name
}
}
Class Movie: MediaItem {
Var director: String
Init (name: String, director: String ){
Self. director = director
Super. init (name: name)
}
}
Class Song: MediaItem {
Var artist: String
Init (name: String, artist: String ){
Self. artist = artist
Super. init (name: name)
}
}
Let library = [
Movie (name: "Casablanca", director: "Michael Curtiz "),
Song (name: "Blue Suede Shoes", artist: "Elvis Presley "),
Astley ")
]
Var movieCount = 0
Var songCount = 0
For item in library {
If item is Movie {
++ MovieCount
} Else if item is Song {
++ SongCount
}
}
For item in library {
If let movie = item? Movie {
Println ("Movie: '\ (movie. name)', dir. \ (movie. director )")
} Else if let song = item? Song {
Println ("Song: '\ (song. name)', by \ (song. artist )")
}
}
In this example, the inheritance tree of a media class is defined. Both Movie and Song classes inherit from their base class MediaItem, and then an array library containing the two media item instances is defined, then, in the for in loop, use the is operator to check whether a media item is of a specific type and use the as operator as an option? To convert the media item instance to a specific type of instance.
2. Use of any type
Swift provides two specific types of aliases: AnyObject and Any.
The AnyObject type can represent any class type.
The Any type can represent Any type other than the function type.
The example shows how to use the AnyObject type to define an array of the AnyObject type and how to use this array. The array Member of the AnyObject type can be an instance of any class. In this example, it is an instance of the Movie class.
Let someObjects: AnyObject [] = [
Movie (name: "2001: A Space Odyssey", director: "Stanley Kubrick "),
Movie (name: "Moon", director: "Duncan Jones "),
]
For object in someObjects {
Let movie = object as Movie
Println ("Movie: '\ (movie. name)', dir. \ (movie. director )")
}
// The above loop uses the as operator to forcibly convert an instance of the AnyObject type to an instance of the specified type Movie class. Therefore, a runtime error may occur.
// You can also directly use the as operator to convert an array of the someObjects type to an array of the Movie type, as shown below:
For movie in someObjects as Movie [] {
Println ("Movie: '\ (movie. name)', dir. \ (movie. director )")
}
The following example shows how to use Any instead of Any type.
Var things = Any [] ()
Things. append (0)
Things. append (0.0)
Things. append ("hello ")
Things. append (3.0, 5.0 ))
Things. append (Movie (name: "Ghostbusters", director: "Ivan Reitman "))
An array of Any type can contain Any type of instances. For example, an array of Any type things contains an integer, floating point number, String, multivariate group, and class instance.
Binary nesting
Type nesting allows you to define another type in one type. The type defined in another type is called the nested type, and the nested type is defined within its supported type. Types can be nested at multiple levels, and Other types can be defined inside the nested types, as shown in the following example:
Struct BlackjackCard {
// Nested Rank enumeration
Enum Rank: Int {
Case Jack, Queen, King, Ace
// Nested Values struct
Struct Values {
Let first: Int, second: Int?
}
}
}
Use the dot syntax to access a nested type of attributes, methods, or subscript.
Starting from the outermost type of the nested type, you can obtain the attributes, methods, or subscript of the nested type to be queried at the first level.
As follows:
Let jackSymbol = BlackjackCard. Rank. Jack. toRaw ()