Swift – 类型别名
变量是用于存储值的命名容器。现在每个值都有它所属的类型,称为数据类型。数据类型是数据的分类,它告诉编译器程序员想要如何使用数据。数据类型可以大致分为三类,原始数据类型、用户定义数据类型和派生数据类型。使用整数值 Swift 提供 int 数据,使用十进制值 Swift 提供 Float 或 Double 数据类型,以及使用字母集合 Swift 提供 String 数据类型。大多数语言都提供了一个关键字,我们可以使用它来引用具有其他名称或别名的数据类型。在 Swift 中,我们使用“typealias”关键字来引用具有另一个名称的数据类型。
在 Swift 中输入别名
Swift 提供了一种功能,我们可以使用它来引用具有其他名称或别名的数据类型。基本上,它为现有类型提供了一个新名称,并且始终记住它不会创建新类型。 Swift 提供了一个关键字“typealias”,类似于 C++ 中的“typedef”关键字。我们可以在 Swift 中为一种数据类型定义多个别名或另一个名称。 typealias 的一般语法如下所示,
句法:
typealias myDataType = dataType
这里,typealias 是关键字,dataType 是原始、用户定义或复杂数据类型之一,myAlias 是dataType 的别名,即,现在我们可以使用名称myDataType 来调用datatype。在 Swift 中,我们可以广泛地为以下数据类型定义别名:
1. 原始数据类型的类型别名
Swift 提供了一个关键字,我们可以使用它来引用具有别名或其他名称的原始数据类型。例如,我们可以使用 myInt 引用 Int 数据类型,使用 myFloat 引用 Float,使用 myDouble 引用 Double。在内部,typealias 不会为现有数据类型创建新的数据类型。它只是为现有类型提供备用名称。
句法:
typealias myDataType = dataType
这里,typealias 是关键字,dataType 是原始数据类型之一,Int、Float、Double 等,myDataType 是 dataType 的别名,即,现在我们可以调用名为 myDataType 的 datatype
例子:
在下面的程序中,我们为原始数据类型使用了别名或其他名称,例如,
- myInt 用于 Int
- 用于浮动的 myFloat
- 双倍的 myDouble
- 字符的 myCharacter
- 字符串的 myString
Swift
// Swift program to illustrate typealias
// for primitive data types
// myInt is an alias to Int
// After this statement using Int or
// myInt is equivalent
typealias myInt = Int
// myFloat is an alias to Float
// After this statement using Float or
// myFloat is equivalent
typealias myFloat = Float
// myDouble is an alias to Double
// After this statement using Double or
// myDouble is equivalent
typealias myDouble = Double
// myCharacter is an alias to Character
// After this statement using Character or
// myCharacter is equivalent
typealias myCharacter = Character
// myDouble is an alias to String
// After this statement using String or
// myString is equivalent
typealias myString = String
// Declaring integerNumber of Int type
// myInt is an alias for Int
var integerNumber : myInt
// Initializing the variable
integerNumber = 5
// Print the value represented by integerNumber
print("integerNumber:", integerNumber)
// Declaring floatNumber of Float type
// myFloat is an alias for Float
var floatNumber : myFloat
// Initializing the variable
floatNumber = 5.12345
// Print the value represented by floatNumber
print("floatNumber:", floatNumber)
// Declaring doubleNumber of Double type
// myDouble is an alias for Double
var doubleNumber : myDouble
// Initializing the variable
doubleNumber = 5.123456789
// Print the value represented by doubleNumber
print("doubleNumber:", doubleNumber)
// Declaring character of Character type
// myCharacter is an alias for Character
var character : myCharacter
// Initializing the variable
character = "A"
// Print the value represented by character
print("character:", character)
// Declaring character of Int type
// myInt is an alias for Int
var string : myString
// Initializing the variable
string = "GeeksforGeeks"
// Print the value represented by string
print("string:", string)
Swift
// Swift program to illustrate typealias for
// user-defined and derived data types
// Defining a structure
struct Employee
{
// Declaring a variable of string type
var employeeName: String
// Declaring a variable of int type
var employeeId: Int
// Defining a custom constructor
init(employeeName: String, employeeId: Int)
{
// Initializing data members with the parameters
// passed to the constructor
self.employeeName = employeeName
self.employeeId = employeeId
}
}
// Defining a class
class ComplexNumber
{
var realPart: Double
var imaginaryPart: Double
init(realPart: Double, imaginaryPart: Double)
{
self.realPart = realPart
self.imaginaryPart = imaginaryPart
}
}
// Defining alias to "Array"
// After this statement using employees or
// Array is equivalent
typealias employees = Array
// myArray is an array of type Employee
var myArray: employees = []
// Instantiate an object of structure
var employee1 = Employee(employeeName: "Bhuwanesh",
employeeId: 131478)
// Instantiate another object of structure
var employee2 = Employee(employeeName: "Harshit",
employeeId: 256478)
// Instantiate another object of structure
var employee3 = Employee(employeeName: "Hitesh",
employeeId: 371948)
// Append Employee objects in the array
myArray.append(employee1)
myArray.append(employee2)
myArray.append(employee3)
// Print array elements
for element in myArray
{
print("Employee Name :", element.employeeName,
",", "Employee Id :" , element.employeeId);
}
Swift
// Swift program to illustrate type alias for complex types
// Alias to function type (Int, Int) -> Int
// After this statement using (Int, Int) -> Int or
// functionType is equivalent
typealias functionType = (Int, Int) -> Int
// Function to multiply variables
func multiply( a: Int, b: Int) -> Int {
return a * b
}
// Assigning multiply function to myFunction
var myfunction: functionType = multiply
// Initializing variables
var myNumber1: Int = 7
var myNumber2: Int = 8
// Calling function through variable
var answer = myfunction(myNumber1, myNumber2)
// Print the value represented by the answer
print("Multiplication of", myNumber1,
"and", myNumber2, "is", answer)
输出:
integerNumber: 5
floatNumber: 5.12345
doubleNumber: 5.123456789
character: A
string: GeeksforGeeks
2. 用户自定义数据类型的类型别名
Swift 提供了一些功能,我们可以使用它来引用具有别名或其他名称的用户定义数据类型。例如,我们可以使用 arrayInt 引用 Array
句法:
typealias myDataType = dataType
这里,dataType 是用户定义的数据类型之一,Array
例子:
在下面的程序中,我们创建了一个“Employee ”类型的结构。它包含两个变量,一个是字符串类型,另一个是 Int 类型。 String 类型变量存储员工的姓名,Int 类型存储员工的标识号。我们为用户定义的数据类型使用了其他名称,例如,“employees”代表“Array
迅速
// Swift program to illustrate typealias for
// user-defined and derived data types
// Defining a structure
struct Employee
{
// Declaring a variable of string type
var employeeName: String
// Declaring a variable of int type
var employeeId: Int
// Defining a custom constructor
init(employeeName: String, employeeId: Int)
{
// Initializing data members with the parameters
// passed to the constructor
self.employeeName = employeeName
self.employeeId = employeeId
}
}
// Defining a class
class ComplexNumber
{
var realPart: Double
var imaginaryPart: Double
init(realPart: Double, imaginaryPart: Double)
{
self.realPart = realPart
self.imaginaryPart = imaginaryPart
}
}
// Defining alias to "Array"
// After this statement using employees or
// Array is equivalent
typealias employees = Array
// myArray is an array of type Employee
var myArray: employees = []
// Instantiate an object of structure
var employee1 = Employee(employeeName: "Bhuwanesh",
employeeId: 131478)
// Instantiate another object of structure
var employee2 = Employee(employeeName: "Harshit",
employeeId: 256478)
// Instantiate another object of structure
var employee3 = Employee(employeeName: "Hitesh",
employeeId: 371948)
// Append Employee objects in the array
myArray.append(employee1)
myArray.append(employee2)
myArray.append(employee3)
// Print array elements
for element in myArray
{
print("Employee Name :", element.employeeName,
",", "Employee Id :" , element.employeeId);
}
输出:
Employee Name : Bhuwanesh , Employee Id : 131478
Employee Name : Harshit , Employee Id : 256478
Employee Name : Hitesh , Employee Id : 371948
3. 复杂数据类型的类型别名
复杂数据类型是由一种以上原始数据类型组成的数据类型。我们也可以为复杂数据类型指定类型别名。例如,我们可以将 (Int, Int) -> (Int) 称为“intIntInt”,将 (Int, Int) -> (Bool) 称为“intIntBool”等。与其他数据类型一样,在内部,typealias 不会创建现有复杂数据类型的新数据类型。它只是为现有的复杂数据类型提供备用名称。
句法:
typealias myDataType = complexDataType
这里,complexDataType 是复杂数据类型之一,(Int) -> (Int), (Int) -> (Float), (Float) -> (Float) 等,myDataType 是 dataType 的别名,即现在我们可以使用名称 myDataType 调用数据类型
例子:
在下面的程序中,我们将两个整数相乘,并将定义的函数(将两个整数相乘)分配给一个复杂的数据类型。我们为复杂数据类型使用了另一个名称或别名,例如“functionType”代表“(Int, Int) -> Int”。
迅速
// Swift program to illustrate type alias for complex types
// Alias to function type (Int, Int) -> Int
// After this statement using (Int, Int) -> Int or
// functionType is equivalent
typealias functionType = (Int, Int) -> Int
// Function to multiply variables
func multiply( a: Int, b: Int) -> Int {
return a * b
}
// Assigning multiply function to myFunction
var myfunction: functionType = multiply
// Initializing variables
var myNumber1: Int = 7
var myNumber2: Int = 8
// Calling function through variable
var answer = myfunction(myNumber1, myNumber2)
// Print the value represented by the answer
print("Multiplication of", myNumber1,
"and", myNumber2, "is", answer)
输出:
Multiplication of 7 and 8 is 56