最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Swift編程中的一些類型轉(zhuǎn)換方法詳解

 更新時(shí)間:2015年11月07日 18:14:11   投稿:goldensun  
這篇文章主要介紹了Swift編程中的一些類型轉(zhuǎn)換方法詳解,是Swift入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下

驗(yàn)證一個(gè)實(shí)例的類型'類型轉(zhuǎn)換'在 Swift 語言編程中。它是用來檢查實(shí)例類型是否屬于特定超類或子類或其自己的層次結(jié)構(gòu)定義。

Swift 類型轉(zhuǎn)換提供兩個(gè)操作符:“is” 檢查值的類型和 'as' 將類型值轉(zhuǎn)換為不同的類型值。 類型轉(zhuǎn)換還檢查實(shí)例類型是否符合特定的協(xié)議一致性標(biāo)準(zhǔn)。

定義一個(gè)類層次結(jié)構(gòu)
類型轉(zhuǎn)換用于檢查實(shí)例的類型或者它屬于特定類型。此外,檢查類和它的子類層次結(jié)構(gòu)來檢查并轉(zhuǎn)換這些實(shí)例,使之作為一個(gè)相同的層次結(jié)構(gòu)。

復(fù)制代碼 代碼如下:

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)")


當(dāng)我們使用 playground 運(yùn)行上面的程序,得到以下結(jié)果。

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

類型檢查
進(jìn)行類型檢查,用 'is' 操作符。在 'is' 操作符檢查類型實(shí)例是否屬于特定的子類型,如果它屬于該實(shí)例返回“true”,否則將返回“false”。

復(fù)制代碼 代碼如下:

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")


當(dāng)我們使用 playground 運(yùn)行上面的程序,得到以下結(jié)果。

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

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

“as!” 返回強(qiáng)制解包裹,如可選鏈,向下轉(zhuǎn)換返回 nil 值。它用來觸發(fā)運(yùn)行時(shí)錯(cuò)誤在向下轉(zhuǎn)型出現(xiàn)故障時(shí)

復(fù)制代碼 代碼如下:

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)")
   }
}


當(dāng)我們使用 playground 運(yùn)行上面的程序,得到以下結(jié)果。

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 

類型轉(zhuǎn)換:任何與任何對象
為了表示實(shí)例屬于任何類型包括函數(shù)類型,使用“Any”關(guān)鍵字

復(fù)制代碼 代碼如下:

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")
   }
}


當(dāng)我們使用 playground 運(yùn)行上面的程序,得到以下結(jié)果。

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

為了表示是任何類型的類實(shí)例,使用AnyObject“關(guān)鍵字

復(fù)制代碼 代碼如下:

 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")
   }
}


當(dāng)我們使用 playground 運(yùn)行上面的程序,得到以下結(jié)果。

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

相關(guān)文章

  • Swift 中如何使用 Option Pattern 改善可選項(xiàng)的 API 設(shè)計(jì)

    Swift 中如何使用 Option Pattern 改善可選項(xiàng)的 API 設(shè)計(jì)

    這篇文章主要介紹了Swift 中如何使用 Option Pattern 改善可選項(xiàng)的 API 設(shè)計(jì),幫助大家更好的進(jìn)行ios開發(fā),感興趣的朋友可以了解下
    2020-10-10
  • Swift可選值優(yōu)化示例詳解

    Swift可選值優(yōu)化示例詳解

    這篇文章主要為大家介紹了Swift可選值優(yōu)化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • 利用swift實(shí)現(xiàn)卡片橫向滑動(dòng)動(dòng)畫效果的方法示例

    利用swift實(shí)現(xiàn)卡片橫向滑動(dòng)動(dòng)畫效果的方法示例

    卡片橫向滑動(dòng)動(dòng)畫效果相信對大家來說都不陌生,下面這篇文章主要給大家介紹了關(guān)于利用swift實(shí)現(xiàn)卡片橫向滑動(dòng)動(dòng)畫效果的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2017-07-07
  • 詳解Swift中的下標(biāo)訪問用法

    詳解Swift中的下標(biāo)訪問用法

    在Swift中我們可以用subscript函數(shù)來定義下標(biāo),從而通過下標(biāo)來訪問數(shù)組與字典等數(shù)據(jù)結(jié)構(gòu),這里我們就來詳解Swift中的下標(biāo)訪問用法:
    2016-07-07
  • R.swift的使用與安裝教程

    R.swift的使用與安裝教程

    這篇文章主要給大家介紹了關(guān)于R.swift使用與安裝的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • Objective-C中的block與Swift中的尾隨閉包使用教程

    Objective-C中的block與Swift中的尾隨閉包使用教程

    Block是OC中的閉包,他和swift中的閉包有什么區(qū)別呢?下面這篇文章就來給大家介紹關(guān)于Objective-C中的block與Swift中的尾隨閉包使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-12-12
  • Swift中的限定擴(kuò)展詳析

    Swift中的限定擴(kuò)展詳析

    擴(kuò)展就是向一個(gè)已有的類、結(jié)構(gòu)體或枚舉類型添加新功能。下面這篇文章主要給大家介紹了關(guān)于Swift中限定擴(kuò)展的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。
    2018-03-03
  • Swift縮放并填充圖片功能的實(shí)現(xiàn)

    Swift縮放并填充圖片功能的實(shí)現(xiàn)

    最近有一個(gè)需求,就是將圖片先等比例縮放到指定大小,然后將空余出來空間填充為黑色,返回指定大小的圖片。本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2021-11-11
  • RxSwift使用技巧之過濾操作詳解

    RxSwift使用技巧之過濾操作詳解

    RxSwift的目的是讓讓數(shù)據(jù)/事件流和異步任務(wù)能夠更方便的序列化處理,能夠使用Swift進(jìn)行響應(yīng)式編程,下面這篇文章主要給大家介紹了關(guān)于RxSwift使用技巧之過濾操作的相關(guān)資料,需要的朋友可以參考下。
    2017-09-09
  • Swift之UITabBarController 導(dǎo)航控制器的自定義

    Swift之UITabBarController 導(dǎo)航控制器的自定義

    本文給大家介紹swift導(dǎo)航控制器之UITabBarController,本文通過代碼實(shí)例給大家講解swift導(dǎo)航控制器,導(dǎo)航控制器類繼承UITabBarController,代碼簡單易懂,需要的朋友可以參考下
    2015-10-10

最新評論

华容县| 临朐县| 昌邑市| 花莲县| 河曲县| 稷山县| 海兴县| 河间市| 西乡县| 大姚县| 忻城县| 夏邑县| 塔河县| 长治县| 昌吉市| 东方市| 永丰县| 建阳市| 句容市| 阿克陶县| 武安市| 盐池县| 郧西县| 临汾市| 清镇市| 德阳市| 明光市| 广河县| 洞头县| 平塘县| 阳泉市| 龙川县| 榆社县| 三都| 安福县| 乐至县| 武邑县| 上犹县| 文昌市| 长宁县| 洛浦县|