- Swift 教程
- Swift - 首頁
- Swift - 概述
- Swift - 環境
- Swift - 基本語法
- Swift - 變數
- Swift - 常量
- Swift - 字面量
- Swift - 註釋
- Swift 運算子
- Swift - 運算子
- Swift - 算術運算子
- Swift - 比較運算子
- Swift - 邏輯運算子
- Swift - 賦值運算子
- Swift - 位運算子
- Swift - 其他運算子
- Swift 高階運算子
- Swift - 運算子過載
- Swift - 算術溢位運算子
- Swift - 恆等運算子
- Swift - 範圍運算子
- Swift 資料型別
- Swift - 資料型別
- Swift - 整數
- Swift - 浮點數
- Swift - Double
- Swift - 布林值
- Swift - 字串
- Swift - 字元
- Swift - 類型別名
- Swift - 可選型別
- Swift - 元組
- Swift - 斷言和前提條件
- Swift 控制流
- Swift - 決策制定
- Swift - if 語句
- Swift - if...else if...else 語句
- Swift - if-else 語句
- Swift - 巢狀 if 語句
- Swift - switch 語句
- Swift - 迴圈
- Swift - for in 迴圈
- Swift - while 迴圈
- Swift - repeat...while 迴圈
- Swift - continue 語句
- Swift - break 語句
- Swift - fall through 語句
- Swift 集合
- Swift - 陣列
- Swift - 集合
- Swift - 字典
- Swift 函式
- Swift - 函式
- Swift - 巢狀函式
- Swift - 函式過載
- Swift - 遞迴
- Swift - 高階函式
- Swift 閉包
- Swift - 閉包
- Swift - 轉義和非轉義閉包
- Swift - 自動閉包
- Swift 面向物件程式設計
- Swift - 列舉
- Swift - 結構體
- Swift - 類
- Swift - 屬性
- Swift - 方法
- Swift - 下標
- Swift - 繼承
- Swift - 重寫
- Swift - 初始化
- Swift - 析構
- Swift 高階
- Swift - ARC 概述
- Swift - 可選鏈
- Swift - 錯誤處理
- Swift - 併發
- Swift - 型別轉換
- Swift - 巢狀型別
- Swift - 擴充套件
- Swift - 協議
- Swift - 泛型
- Swift - 訪問控制
- Swift - 函式與方法
- Swift - SwiftyJSON
- Swift - 單例類
- Swift 隨機數
- Swift 不透明型別和裝箱型別
- Swift 有用資源
- Swift - 線上編譯
- Swift - 快速指南
- Swift - 有用資源
- Swift - 討論
Swift - 類型別名
Swift 提供了一個名為類型別名的特殊功能。類型別名用於為預定義型別定義另一個名稱或別名。它僅為現有型別提供一個新名稱,而不會建立新的型別。例如,“Int”也可以定義為“myInt”。
定義類型別名後,我們可以在程式中的任何位置使用該別名。單個程式可以包含多個別名。類型別名通常用於提高程式碼可讀性、程式碼質量和抽象複雜型別。我們可以藉助**type alias**關鍵字定義類型別名。
語法
以下是類型別名的語法:
typealiase newName = existingType
我們可以為以下資料型別定義類型別名:
原始資料型別
使用者定義的資料型別
複雜資料型別
Swift 中原始資料型別的類型別名
原始資料型別是 Swift 提供的預定義資料型別,例如 Int、Float、Double、Character 和 String。藉助類型別名,我們可以為內建資料型別提供一個新名稱,該名稱可以在整個程式中使用而不會出現任何錯誤。例如,“typealias myString = String”,現在使用 myString 我們可以建立一個字串型別的變數。
語法
以下是原始資料型別類型別名的語法:
typealias newName = PreDefinedDataType
此處,PreDefinedDataType 可以是 Int、Float、Double、String 和 Character。
示例
Swift 程式,用於為內建資料型別建立類型別名。
import Foundation
// Creating type typealias for String
typealias myString = String
// Creating type typealias for Float
typealias myNum = Float
// Creating type typealias for Int
typealias Num = Int
// Declaring integer type variable using Num
var number : Num = 10
// Declaring string type variable using myString
var newString : myString = "Tutorialspoint"
// Declaring float type variable using myNum
var value : myNum = 23.456
print("Number:", number)
print("type:", type(of: number))
print("\nString:", newString)
print("type:", type(of: newString))
print("\nFloat:", value)
print("type:", type(of: value))
輸出
Number: 10 type: Int String: Tutorialspoint type: String Float: 23.456 type: Float
Swift 中使用者定義的資料型別的類型別名
使用類型別名,我們還可以為使用者定義的資料型別提供備用名稱。例如,**"typealias mySet = Set<String>"**,現在使用 mySet 我們可以建立一個字串型別的集合。
語法
以下是使用者定義的資料型別類型別名的語法:
typealias newName = dataType
此處,datatype 可以是 Array、Set、dictionary 等。
示例
Swift 程式,用於為使用者定義的資料型別建立類型別名。
import Foundation
// Creating type typealias for Set
typealias mySet = Set<Int>
// Creating type typealias for Array
typealias myNum = Array<Int>
// Creating type typealias for Array
typealias Num = Array<String>
// Declaring set of integer type using mySet
var newSet : mySet = [32, 3, 1, 2]
// Declaring array of integer type using myNum
var newArray : myNum = [32, 2, 1, 1, 3]
// Declaring array of string type using Num
var newArr : Num = ["Swift", "C++", "C#"]
print("Set:", newSet)
print("Array:", newArray)
print("Array:", newArr)
輸出
Set: [32, 3, 1, 2] Array: [32, 2, 1, 1, 3] Array: ["Swift", "C++", "C#"]
Swift 中複雜資料型別的類型別名
複雜資料是包含多個預定義資料型別的特殊型別的資料型別。因此,使用類型別名,我們還可以建立複雜資料型別的別名。例如,**(String, String) -> string** 作為“MyString”。
語法
以下是複雜資料型別類型別名的語法:
typealias newName = CdataType
此處,CdataType 可以是任何複雜資料型別,例如 (String)->(String)。
示例
Swift 程式,用於為複雜資料型別建立類型別名。
import Foundation
// Creating type typealias for function type
typealias Value = (String, String) -> String
func addStr(s1: String, s2: String) -> String{
return s1 + s2
}
// Assigning addStr function to Value
var newFunc : Value = addStr
// Calling function
var result = newFunc("Hello", "World")
print(result)
輸出
HelloWorld