亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > Swift > 正文

Swift編程中的一些類型轉換方法詳解

2020-03-09 17:52:52
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了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 physicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance 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 physicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance formulae is: Giga HertzSubjects 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 physicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance formulae is: Giga HertzChemistry topics are: 'solid physics', HertzMaths topics are: 'Fluid Dynamics', Giga HertzChemistry topics are: 'Thermo physics', DecibelsMaths topics are: 'Astro Physics', MegaHertzMaths 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 physicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance formulae is: Giga HertzChemistry topics are: 'solid physics', HertzMaths topics are: 'Fluid Dynamics', Giga HertzChemistry topics are: 'Thermo physics', DecibelsMaths topics are: 'Astro Physics', MegaHertzMaths topics are: 'Differential Equations', Cosine SeriesInteger value is 12Pi value is 3.14159Example for AnyTopics 'solid physics', HertzAnyObject

為了表示是任何類型的類實例,使用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 physicsInstance equation is: HertzInstance physics is: Fluid DynamicsInstance formulae is: Giga HertzChemistry topics are: 'solid physics', HertzMaths topics are: 'Fluid Dynamics', Giga HertzChemistry topics are: 'Thermo physics', DecibelsMaths topics are: 'Astro Physics', MegaHertzMaths topics are: 'Differential Equations', Cosine SeriesInteger value is 12Pi value is 3.14159Example for AnyTopics 'solid physics', Hertz


注:相關教程知識閱讀請移步到swift教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩精品免费在线视频| 欧美成人精品三级在线观看| 欧美多人乱p欧美4p久久| 欧美丰满少妇xxxxx| 蜜臀久久99精品久久久久久宅男| 日韩专区在线播放| 国外成人免费在线播放| 国产精品嫩草影院一区二区| 成人中心免费视频| 国产裸体写真av一区二区| 日本欧美中文字幕| 国产精品免费网站| 欧美日韩视频免费播放| 激情成人在线视频| 亚洲国产另类久久精品| 成人黄色午夜影院| 色七七影院综合| 午夜精品一区二区三区在线视频| 日韩电影大全免费观看2023年上| 亚洲欧美自拍一区| 国产精品三级久久久久久电影| 国产精品v片在线观看不卡| 欧美亚洲视频一区二区| 性欧美长视频免费观看不卡| 欧美激情2020午夜免费观看| 自拍偷拍亚洲欧美| 都市激情亚洲色图| 成人乱人伦精品视频在线观看| 日韩黄色av网站| 精品久久香蕉国产线看观看亚洲| 欧美日韩人人澡狠狠躁视频| 亚洲大尺度美女在线| 国产精品69精品一区二区三区| 一本大道久久加勒比香蕉| y97精品国产97久久久久久| 国产成人av在线| 日本高清+成人网在线观看| 91亚洲精华国产精华| 欧美自拍视频在线| 日本乱人伦a精品| 欧美日韩成人精品| 中文在线资源观看视频网站免费不卡| 成人h片在线播放免费网站| 欧美日韩成人在线视频| 亚洲无限乱码一二三四麻| 国产视频精品va久久久久久| 国产成人一区二区三区小说| 日本精品一区二区三区在线| 欧美在线免费看| 美日韩精品免费观看视频| 日韩精品一区二区三区第95| 国产亚洲视频中文字幕视频| 亚洲中国色老太| 中文字幕日韩在线视频| 色综合久久天天综线观看| 欧美电影免费观看高清| 91探花福利精品国产自产在线| 亚洲欧美日韩精品久久奇米色影视| 国产欧美亚洲精品| 欧美精品久久久久久久免费观看| 久久精品成人欧美大片古装| 国产欧美精品日韩| 日韩在线中文视频| 精品高清美女精品国产区| 日韩av中文字幕在线播放| 国产一级揄自揄精品视频| 91精品在线一区| 国产精品无码专区在线观看| 国产精自产拍久久久久久蜜| 成人激情免费在线| 久久躁狠狠躁夜夜爽| 亚洲国产一区二区三区在线观看| 国产精品小说在线| 久久免费视频在线观看| 精品国内产的精品视频在线观看| 亚洲第一视频网| 成人免费视频a| 日韩精品中文字幕久久臀| 国产精品678| 国产精品高潮呻吟久久av野狼| 九九热在线精品视频| 亚洲最新在线视频| 国产精品人成电影| 亚洲经典中文字幕| 一区二区亚洲精品国产| 欧美自拍视频在线观看| 国产精品日韩久久久久| 久久精品中文字幕一区| 日韩精品在线视频观看| 午夜精品久久久久久久久久久久| 岛国av午夜精品| 午夜精品免费视频| 久久成人综合视频| 91精品国产一区| 亚洲精品99久久久久中文字幕| 精品久久久久久久久久久久久久| 亚洲国产精品久久久久秋霞不卡| 91亚洲精品久久久久久久久久久久| 亚洲男人的天堂网站| 日韩成人激情在线| 亚洲一区二区在线播放| 少妇av一区二区三区| 色yeye香蕉凹凸一区二区av| 久久亚洲私人国产精品va| 精品国产乱码久久久久久婷婷| 精品动漫一区二区三区| 精品综合久久久久久97| 播播国产欧美激情| 在线观看中文字幕亚洲| 伦伦影院午夜日韩欧美限制| 久久免费视频观看| 欧美一区二区视频97| 国产一区二区三区久久精品| 国产日本欧美一区二区三区在线| 免费97视频在线精品国自产拍| 国产精品露脸自拍| 91精品视频在线播放| 久久av在线看| 国产亚洲精品91在线| 97在线视频精品| 欧美三级欧美成人高清www| 亚洲va码欧洲m码| 国产精品久久久久久久7电影| 欧美国产日产韩国视频| 清纯唯美亚洲激情| 欧美精品成人91久久久久久久| 国产精品香蕉av| 成人激情在线观看| 福利二区91精品bt7086| 高潮白浆女日韩av免费看| 午夜精品久久久久久久99热| 欧美激情18p| 亚洲综合一区二区不卡| 在线观看国产精品日韩av| 国产精品色午夜在线观看| 亚洲男人天堂古典| 日韩女在线观看| 欧美大尺度电影在线观看| 91久久精品国产91久久性色| 日韩精品一二三四区| 日韩一区二区久久久| 日韩国产欧美区| 亚洲欧美日韩天堂一区二区| 久久久日本电影| 精品成人国产在线观看男人呻吟| 久久91精品国产91久久跳| 亚洲视频在线观看免费| 精品国偷自产在线视频| 日韩激情第一页| 亚洲第一网站免费视频| 久久97精品久久久久久久不卡| 91成人在线播放| 欧美黑人极品猛少妇色xxxxx| 国产亚洲欧美另类中文| 亚洲福利影片在线| 国产97在线亚洲| 亚洲欧洲第一视频| 国产91精品不卡视频| 亚洲成人久久网| 精品一区二区三区三区| 国产亚洲人成a一在线v站| 亚洲精品国产品国语在线| 亚洲欧美日韩第一区| 欧美午夜丰满在线18影院|