Swift教學
Swift快速入門
Swift簡介
Swift基礎
Swift常量和變量
Swift註釋
swift分號
Swift整數
Swift浮點數
Swift類型安全和類型推斷
swift數值型字面量
Swift數值型類型轉換
Swift類型別名
Swift布爾值
Swift元組
Swift可選類型
Swift斷言
Swift基本運算符
Swif哈希集合
Swift運算術語
Swift字典
Swift賦值運算符
Swift數值運算
Swift複合賦值
Swift比較運算
Swift三元條件運算
Swift區間運算符
Swift邏輯運算
Swift字符串和字符
Swift字符串字面量
Swift初始化空字符串
Swift字符串可變性
Swift字符串是值類型
Swift使用字符
Swift計算字符數量
Swift連接字符串和字符
Swift字符串插值
Swift比較字符串
Swift大寫和小寫字符串
Swift Unicode
Swift集合類型 (Collection Types)
Swift數組
Swift字典
Swift集合的可變性
Swift控制流
Swift For循環
Swift While循環
Swift條件語句
Swift控制轉移語句
Swift函數
Swift函數的定義與調用
Swift函數參數與返回值
Swift函數參數名稱
Swift函數類型
Swift嵌套函數
Swift閉包
Swift閉包表達式
Swift尾隨閉包
Swift捕獲值
Swift閉包是引用類型
Swift枚舉
Swift枚舉語法
Swift匹配枚舉值和Switch語句
Swift相關值
Swift原始值
Swift類和結構體
Swift類和結構體對比
Swift結構體和枚舉是值類型
Swift類是引用類型
Swift類和結構體的選擇
Swift集合(Collection)類型的賦值和拷貝行爲
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析構過程原理
Swift析構函數操作
Swift自動引用計數
Swift自動引用計數的工作機制
Swift自動引用計數實踐
Swift類實例之間的循環強引用
Swift解決實例之間的循環強引用
Swift閉包引起的循環強引用
Swift解決閉包引起的循環強引用
Swift可選鏈
Swift可選鏈可替代強制解析
Swift爲可選鏈定義模型類
Swift通過可選鏈調用屬性
Swift通過可選鏈調用方法
Swift使用可選鏈調用子腳本
Swift連接多層鏈接
Swift鏈接可選返回值的方法
Swift類型轉換
Swift定義一個類層次作爲例子
Swift檢查類型
Swift向下轉型
Swift Any和AnyObject類型轉換
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泛型函數
Swift類型參數
Swift命名類型參數
Swift泛型類型
Swift類型約束
Swift關聯類型
Swift Where語句
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二元表達式
Swift賦值表達式
Swift類型轉換運算符
Swift主表達式
Swift後綴表達式
Swift語句
Swift循環語句
Swift For語句
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元組模式
Swift枚舉用例模式
Swift類型轉換模式
Swift表達式模式
Swift泛型參數
Swift泛型形參子句
Swift開發環境設置
Swift基本語法
Swift數據類型
Swift變量
Swift常量
Swift字面量
Swift運算符
Swift比較運算符
Swift邏輯運算符
Swift位運算符
Swift賦值運算符
Swift範圍運算符
Swift其它運算符
Swift運算符優先級
Swift算術運算符
Swift if語句
Swift if...else語句
Swift if...else if...else語句
Swift嵌套 if 語句
Swift Switch語句
Swift決策
Swift for-in循環
Swift for循環
Swift while循環
Swift do...while循環
Swift continue語句
Swift break語句
Swift fallthrough語句
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 類型轉換提供兩個操作符:「is」 檢查值的類型和 'as' 將類型值轉換爲不同的類型值。 類型轉換還檢查實例類型是否符合特定的協議一致性標準。

定義一個類層次結構

類型轉換用於檢查實例的類型或者它屬於特定類型。此外,檢查類和它的子類層次結構來檢查並轉換這些實例,使之作爲一個相同的層次結構。

class Subjects { var physics: String init(physics: String) { self.physics = physics } } class Chemistry: Subjects { var equations: String init(physics: String, equations: String) { self.equations = equations super.init(physics: physics) } } class Maths: Subjects { var formulae: String init(physics: String, formulae: String) { self.formulae = formulae super.init(physics: physics) } } let sa = [ Chemistry(physics: "solid physics", equations: "Hertz"), Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz")] let samplechem = Chemistry(physics: "solid physics", equations: "Hertz") println("Instance physics is: \(samplechem.physics)") println("Instance equation is: \(samplechem.equations)") let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") println("Instance physics is: \(samplemaths.physics)") println("Instance formulae is: \(samplemaths.formulae)")

當我們使用 playground 運行上面的程序,得到以下結果。

Instance physics is: solid physics
Instance equation is: Hertz
Instance physics is: Fluid Dynamics
Instance formulae is: Giga Hertz

類型檢查

進行類型檢查,用 'is' 操作符。在 'is' 操作符檢查類型實例是否屬於特定的子類型,如果它屬於該實例返回「true」,否則將返回「false」。

class Subjects { var physics: String init(physics: String) { self.physics = physics } } class Chemistry: Subjects { var equations: String init(physics: String, equations: String) { self.equations = equations super.init(physics: physics) } } class Maths: Subjects { var formulae: String init(physics: String, formulae: String) { self.formulae = formulae super.init(physics: physics) } } let sa = [ Chemistry(physics: "solid physics", equations: "Hertz"), Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz"), Chemistry(physics: "Thermo physics", equations: "Decibels"), Maths(physics: "Astro Physics", formulae: "MegaHertz"), Maths(physics: "Differential Equations", formulae: "Cosine Series")] let samplechem = Chemistry(physics: "solid physics", equations: "Hertz") println("Instance physics is: \(samplechem.physics)") println("Instance equation is: \(samplechem.equations)") let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") println("Instance physics is: \(samplemaths.physics)") println("Instance formulae is: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in sa { if item is Chemistry { ++chemCount } else if item is Maths { ++mathsCount } } println("Subjects in chemistry contains \(chemCount) topics and maths contains \(mathsCount) topics")

當我們使用 playground 運行上面的程序,得到以下結果。

Instance physics is: solid physics
Instance equation is: Hertz
Instance physics is: Fluid Dynamics
Instance formulae is: Giga Hertz
Subjects in chemistry contains 2 topics and maths contains 3 topics

向下轉換

向下類型轉換的子類型可以有兩個操作符(如:as? 和 as!)。as? 當值是nil,返回一個可選值。它是用來檢查成功向下轉型。

「as!」 返回強制解包裹,如可選鏈,向下轉換返回 nil 值。它用來觸發運行時錯誤在向下轉型出現故障時

class Subjects { var physics: String init(physics: String) { self.physics = physics } } class Chemistry: Subjects { var equations: String init(physics: String, equations: String) { self.equations = equations super.init(physics: physics) } } class Maths: Subjects { var formulae: String init(physics: String, formulae: String) { self.formulae = formulae super.init(physics: physics) } } let sa = [ Chemistry(physics: "solid physics", equations: "Hertz"), Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz"), Chemistry(physics: "Thermo physics", equations: "Decibels"), Maths(physics: "Astro Physics", formulae: "MegaHertz"), Maths(physics: "Differential Equations", formulae: "Cosine Series")] let samplechem = Chemistry(physics: "solid physics", equations: "Hertz") println("Instance physics is: \(samplechem.physics)") println("Instance equation is: \(samplechem.equations)") let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") println("Instance physics is: \(samplemaths.physics)") println("Instance formulae is: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in sa { if let print = item as? Chemistry { println("Chemistry topics are: '\(print.physics)', \(print.equations)") } else if let example = item as? Maths { println("Maths topics are: '\(example.physics)', \(example.formulae)") } }

當我們使用 playground 運行上面的程序,得到以下結果。

Instance physics is: solid physics
Instance equation is: Hertz
Instance physics is: Fluid Dynamics
Instance formulae is: Giga Hertz
Chemistry topics are: 'solid physics', Hertz
Maths topics are: 'Fluid Dynamics', Giga Hertz
Chemistry topics are: 'Thermo physics', Decibels
Maths topics are: 'Astro Physics', MegaHertz
Maths topics are: 'Differential Equations', Cosine Series 

類型轉換:任何與任何對象

爲了表示實例屬於任何類型包括函數類型,使用「Any」關鍵字

class Subjects { var physics: String init(physics: String) { self.physics = physics } } class Chemistry: Subjects { var equations: String init(physics: String, equations: String) { self.equations = equations super.init(physics: physics) } } class Maths: Subjects { var formulae: String init(physics: String, formulae: String) { self.formulae = formulae super.init(physics: physics) } } let sa = [ Chemistry(physics: "solid physics", equations: "Hertz"), Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz"), Chemistry(physics: "Thermo physics", equations: "Decibels"), Maths(physics: "Astro Physics", formulae: "MegaHertz"), Maths(physics: "Differential Equations", formulae: "Cosine Series")] let samplechem = Chemistry(physics: "solid physics", equations: "Hertz") println("Instance physics is: \(samplechem.physics)") println("Instance equation is: \(samplechem.equations)") let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") println("Instance physics is: \(samplemaths.physics)") println("Instance formulae is: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in sa { if let print = item as? Chemistry { println("Chemistry topics are: '\(print.physics)', \(print.equations)") } else if let example = item as? Maths { println("Maths topics are: '\(example.physics)', \(example.formulae)") } } var exampleany = [Any]() exampleany.append(12) exampleany.append(3.14159) exampleany.append("Example for Any") exampleany.append(Chemistry(physics: "solid physics", equations: "Hertz")) for print in exampleany { switch print { case let someInt as Int: println("Integer value is \(someInt)") case let someDouble as Double where someDouble > 0: println("Pi value is \(someDouble)") case let someString as String: println("\(someString)") case let phy as Chemistry: println("Topics '\(phy.physics)', \(phy.equations)") default: println("None") } }

當我們使用 playground 運行上面的程序,得到以下結果。

Instance physics is: solid physics
Instance equation is: Hertz
Instance physics is: Fluid Dynamics
Instance formulae is: Giga Hertz
Chemistry topics are: 'solid physics', Hertz
Maths topics are: 'Fluid Dynamics', Giga Hertz
Chemistry topics are: 'Thermo physics', Decibels
Maths topics are: 'Astro Physics', MegaHertz
Maths topics are: 'Differential Equations', Cosine Series
Integer value is 12
Pi value is 3.14159
Example for Any
Topics 'solid physics', Hertz

AnyObject

爲了表示是任何類型的類實例,使用AnyObject「關鍵字

class Subjects { var physics: String init(physics: String) { self.physics = physics } } class Chemistry: Subjects { var equations: String init(physics: String, equations: String) { self.equations = equations super.init(physics: physics) } } class Maths: Subjects { var formulae: String init(physics: String, formulae: String) { self.formulae = formulae super.init(physics: physics) } } let saprint: [AnyObject] = [Chemistry(physics: "solid physics", equations: "Hertz"), Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz"), Chemistry(physics: "Thermo physics", equations: "Decibels"), Maths(physics: "Astro Physics", formulae: "MegaHertz"), Maths(physics: "Differential Equations", formulae: "Cosine Series")] let samplechem = Chemistry(physics: "solid physics", equations: "Hertz") println("Instance physics is: \(samplechem.physics)") println("Instance equation is: \(samplechem.equations)") let samplemaths = Maths(physics: "Fluid Dynamics", formulae: "Giga Hertz") println("Instance physics is: \(samplemaths.physics)") println("Instance formulae is: \(samplemaths.formulae)") var chemCount = 0 var mathsCount = 0 for item in saprint { if let print = item as? Chemistry { println("Chemistry topics are: '\(print.physics)', \(print.equations)") } else if let example = item as? Maths { println("Maths topics are: '\(example.physics)', \(example.formulae)") } } var exampleany = [Any]() exampleany.append(12) exampleany.append(3.14159) exampleany.append("Example for Any") exampleany.append(Chemistry(physics: "solid physics", equations: "Hertz")) for print in exampleany { switch print { case let someInt as Int: println("Integer value is \(someInt)") case let someDouble as Double where someDouble > 0: println("Pi value is \(someDouble)") case let someString as String: println("\(someString)") case let phy as Chemistry: println("Topics '\(phy.physics)', \(phy.equations)") default: println("None") } }

當我們使用 playground 運行上面的程序,得到以下結果。

Instance physics is: solid physics
Instance equation is: Hertz
Instance physics is: Fluid Dynamics
Instance formulae is: Giga Hertz
Chemistry topics are: 'solid physics', Hertz
Maths topics are: 'Fluid Dynamics', Giga Hertz
Chemistry topics are: 'Thermo physics', Decibels
Maths topics are: 'Astro Physics', MegaHertz
Maths topics are: 'Differential Equations', Cosine Series
Integer value is 12
Pi value is 3.14159
Example for Any
Topics 'solid physics', Hertz