日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

初探swift语言的学习笔记七(swift 的关健词)

發布時間:2024/9/30 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 初探swift语言的学习笔记七(swift 的关健词) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
作者:fengsh998 原文地址:http://blog.csdn.net/fengsh998/article/details/32133809 轉載請注明出處 如果覺得文章對你有所幫助,請通過留言或關注微信公眾帳號fengsh998來支持我,謝謝!


每一種語言都有相應的關鍵詞,每個關鍵詞都有他獨特的作用,來看看swfit中的關鍵詞:

? ??關鍵詞:

用來聲明的:

“ class, deinit, enum, extension, func, import, init, let, protocol, static, struct, subscript, typealias, var.”

用于子句的:

“ break, case, continue, default, do, else, fallthrough, if, in, for, return, switch, where, while.”

表達式和類型的:

“ as, dynamicType, is, new, super, self, __COLUMN__, __FILE__, __FUNCTION__, __LINE__”

//特殊語境使用的:

“didSet, get, inout, mutating, override, set, unowned, unowned(safe), unowned(unsafe), weak , willSet”


class

用來定義一個類,相信大家并不陌生。

如果定義一個汽車類

[cpp]?view plaincopy
  • class?Car??
  • {??
  • ??????init()??
  • ??????{??
  • ????????????//to?do?init?something.??
  • ???????}??
  • }??

  • init

    相對于類的構造方法的修飾。


    deinit

    相對于類的釋構方法的修飾。

    對于類的構造和釋構在swift 中需要使用關鍵詞來修飾,而很多高級語言并不需要特別的指定,便C++ 只需要類名與構造函數名相同就可以,不需要額外的關鍵詞。


    enum

    ?枚舉類型的聲明,這個與很多語方都相通。


    extension

    擴展,有點像oc中的categories 。

    Swift 中的可以擴展以下幾個:
    添加計算型屬性和計算靜態屬性
    定義實例方法和類型方法
    提供新的構造器
    定義下標
    定義和使用新的嵌套類型
    使一個已有類型符合某個接口


    如下面擴展字符串:

    [cpp]?view plaincopy
  • extension?String{??
  • ????struct?_Dummy?{??
  • ????????var?idxVal:?Int??
  • ????????var?_padding:?Int??
  • ????????var?_padding2:?Int??
  • ????????var?_padding3:?Int??
  • ????}??
  • ????//過慮出數字??
  • ????func?fitlerCharater()?->?String??
  • ????{??
  • ????????var?numberstr?:?String?=?""??
  • ????????for?character?in?self??
  • ????????{??
  • ????????????let?s?:String?=?String(character)??
  • ??????????????
  • ????????????//println(s.toInt())??
  • ????????????if?let?hs?=?s.toInt()??
  • ????????????{??
  • ????????????????numberstr?+=?character??
  • ????????????}??
  • ????????}??
  • ????????return?numberstr??
  • ????}??
  • ??????
  • ????//擴展使用下標訪問??
  • ????subscript?(i:?Int)?->?Character?{??
  • ????????var?dummy:?_Dummy?=?reinterpretCast(i?>=?0???self.startIndex?:?self.endIndex)??
  • ????????????dummy.idxVal?+=?i??
  • ????????????let?idx:?String.Index?=?reinterpretCast(dummy)??
  • ????????????return?self[idx]??
  • ????}??
  • ??????
  • ????//擴展使用Range訪問??
  • ????subscript?(subRange:?Range<Int>)?->?String?{??
  • ????????var?start:?_Dummy?=?reinterpretCast(self.startIndex)??
  • ????????????var?end?=?start??
  • ????????????start.idxVal?=?subRange._startIndex??
  • ????????????end.idxVal?=?subRange._endIndex??
  • ????????????let?startIndex:?String.Index?=?reinterpretCast(start)??
  • ????????????let?endIndex:?String.Index?=?reinterpretCast(end)??
  • ????????????return?self[startIndex..endIndex]??
  • ????}??
  • }??

  • 測試:

    [cpp]?view plaincopy
  • func?testExtension()??
  • {??
  • ????var?str?:?String?=?"1234ab5國6cd7中8i90"??
  • ????println(str.fitlerCharater())??
  • ??????
  • ????let?china:?String?=?"china?operating?system?public?to?世界"??
  • ????println("使用下標索引訪問第13個字符?\(china[13])")??
  • ????println("使用負號下標即變為從右往左訪問字符?\(china[-1])")??
  • ????println("使用負號下標即變為從右往左訪問字符?\(china[-2])")??
  • ????println("使用下標Range來訪問范圍?\(china[2...6])")??
  • ????dump(china[1..5],?name:?"china[1:4]")??????????????//使用dump輸出??
  • ????dump(china[10...13],?name:?"china[10:13]")??
  • }??

  • 輸出:

    [cpp]?view plaincopy
  • 1234567890??
  • 使用下標索引訪問第13個字符?n??
  • 使用負號下標即變為從右往左訪問字符?界??
  • 使用負號下標即變為從右往左訪問字符?世??
  • 使用下標Range來訪問范圍?ina?o??
  • -?china[1:4]:?hina??
  • -?china[10:13]:?atin??

  • func

    ?用來修飾函數的關鍵詞。

    import

    ?導入頭文件,相信大家都不陌生,但在swift 中好像被用來導入包,如import UIKit。 因為swift中沒有了頭文件的概念。

    let

    用來修改某一常量的關鍵詞。像const 限定差不多

    var

    用來聲明變量。

    protocol

    協議,也有稱為接口,這個往往在很多高級語言中不能多重繼承的情況下使用協議是一個比較好的多態方式。

    static

    用來修飾變量或函數為靜態

    struct

    用來修飾結構體。

    subscript

    下標修飾,可以使類(class),結構體(struct),枚舉(enum) 使用下標訪問。

    [cpp]?view plaincopy
  • class?Garage??
  • {??
  • ????var?products?:?String[]?=?Array()??
  • ??????
  • ????subscript(index:Int)?->?String??
  • ????{??
  • ????????get??
  • ????????{??
  • ????????????return?products[index]??
  • ????????}??
  • ??????????
  • ????????set??
  • ????????{??
  • ????????????if?index?<?products.count??//&&?!products.isEmpty??
  • ????????????{??
  • ????????????????products[index]?=?newValue??
  • ????????????}??
  • ????????????else??
  • ????????????{??
  • ????????????????products.append(newValue)??
  • ????????????}??
  • ??????????????
  • ????????}??
  • ????}??
  • }??

  • 測試:

    [cpp]?view plaincopy
  • func?testSubscript()??
  • {??
  • ????var?garage?=?Garage()??
  • ????garage[0]?=?"A"??
  • ????garage[1]?=?"B"??
  • ????garage[2]?=?"C"??
  • ????garage[3]?=?"D"??
  • ????garage[2]?=?"CC"??
  • ??????
  • ????println("index?1?=?\(garage[0])?,index?2?=?\(garage[1]),index?3?=?\(garage[2])?,index?4?=?\(garage[3])")??
  • }??

  • 輸出

    [cpp]?view plaincopy
  • index?1?=?A?,index?2?=?B,index?3?=?CC?,index?4?=?D??

  • typealias

    類型別名,就像typedef一樣。借typedef ?unsigned long int ? ?UInt64?

    同樣在swift中也可能自定義類型。


    break

    跳出循環,通常用于for,while,do-while,switch?

    case

    case相信大家并不陌生,常在switch中使用,但如今在swift中多了一個地方使用哪就是枚舉類型。

    continue

    跳過本次循環,繼續往后執行。

    default

    缺省聲明。常見在switch中。

    do, else,if, for, return, switch, while

    這幾個就不用多說了,越說越混。

    in

    范圍或集合操作

    [cpp]?view plaincopy
  • let?str?=?"123456"??
  • for?c?in?str??
  • {??
  • ?????println(c)??
  • }??

  • fallthrough

    由于swift中的switch語句中可以省去了break的寫法,但在其它語言中省去break里,會繼續往后一個case跑,直到碰到break或default才完成。在這里fallthrough就如同其它語言中忘記寫break一樣的功效。

    [cpp]?view plaincopy
  • let?integerToDescribe?=?1??
  • var?description?=?"The?number?\(integerToDescribe)?is"??
  • switch?integerToDescribe?{??
  • case?1,?3,?5,?7,?11,?13,?17,?19:??
  • ????description?+=?"?a?prime?number,?and?also";??
  • ????fallthrough??
  • case?5:??
  • ????description?+=?"?an?integer"??
  • default?:??
  • ????description?+=?"?finished"??
  • }??
  • ??
  • println(description)??

  • 輸出:

    [cpp]?view plaincopy
  • The?number?1?is?a?prime?number,?and?also?an?integer??

  • where

    swift中引入了where 來進行條件判斷。

    [cpp]?view plaincopy
  • let?yetAnotherPoint?=?(1,?-1)??
  • switch?yetAnotherPoint?{??
  • case?let?(x,?y)?where?x?==?y:??
  • println("(\(x),?\(y))?is?on?the?line?x?==?y")??
  • case?let?(x,?y)?where?x?==?-y:??
  • println("(\(x),?\(y))?is?on?the?line?x?==?-y")??
  • case?let?(x,?y):??
  • println("(\(x),?\(y))?is?just?some?arbitrary?point")??
  • }??

  • 當switch的條件滿足where 后面的條件時,才執行語句。

    is

    as

    is 常用于對某變量類型的判斷,就像OC中 isKindClass ,as 就有點像強制類型轉換的意思了。

    [cpp]?view plaincopy
  • for?view?:?AnyObject?in?self.view.subviews??
  • {??
  • ????if?view?is?UIButton??
  • ????{??
  • ????????let?btn?=?view?as?UIButton;??
  • ????????println(btn)??
  • ????}??
  • }??

  • OC的寫法:

    [cpp]?view plaincopy
  • for?(UIView?*view??in?self.view.subviews)??
  • {??
  • ??????if?([view?isKindOfClass:[UIButton?class]])?????????//is?操作??
  • ?????{??
  • ?????????????UIButton?*btn?=(UIButton?*)view?????????????//as?操作??
  • ??????}??
  • }??

  • super

    基類的關鍵語,通常稱父類

    __COLUMN__, __FILE__, __FUNCTION__, __LINE__

    是不是有點像宏定義啊。

    [cpp]?view plaincopy
  • println(__COLUMN__?,__FILE__,?__FUNCTION__,?__LINE__)??

  • 輸出:

    [cpp]?view plaincopy
  • (17,?/Users/apple/Desktop/swiftDemo/swiftDemo/ViewController.swift,?viewDidLoad(),?62)??

  • set,get

    常用于類屬性的setter getter操作。

    willSet,didSet

    在swift中對set操作進行了擴展,willset 在set新值成功前發生,didset在設置新值成功后發生。

    inout

    對函數參數作為輸出參數進行修飾。

    [cpp]?view plaincopy
  • func?swapTwoInts(inout?a:?Int,?inout?b:?Int)?{??
  • ????let?temporaryA?=?a??
  • ????a?=?b??
  • ????b?=?temporaryA??
  • }??

  • mutating

    具體不是很理解,好像是專為結構體使用而設置的變體聲明

    [cpp]?view plaincopy
  • protocol?ExampleProtocol?{??
  • ????var?simpleDescription:?String?{?get?}??
  • ????mutating?func?adjust()??
  • }??
  • ??
  • class?SimpleClass:?ExampleProtocol?{??
  • ????var?simpleDescription:?String?=?"A?very?simple?class."??
  • ????func?adjust()?{??
  • ????????simpleDescription?+=?"??Now?100%?adjusted."??
  • ????}??
  • }??
  • ??
  • ??
  • struct?SimpleStructure:?ExampleProtocol?{??
  • ????var?simpleDescription:?String?=?"A?simple?structure"??
  • ????mutating?func?adjust()?{????????????????//如果去除mutating?報Could?not?find?an?overload?for?'+='?that?accepts?the?supplied?arguments??
  • ????????simpleDescription?+=?"?(adjusted)"??
  • ????}??
  • }??
  • 測試
    [cpp]?view plaincopy
  • func?testMutating()??
  • {??
  • ????var?a?=?SimpleClass()??
  • ????a.adjust()??
  • ????let?aDescription?=?a.simpleDescription??
  • ????println(aDescription)??
  • ??????
  • ????var?b?=?SimpleStructure()??
  • ????b.adjust()??
  • ????let?bDescription?=?b.simpleDescription??
  • ????println(bDescription)??
  • }??
  • override
    父子類之間的函數重寫,即復蓋。

    unowned, unowned(safe), unowned(unsafe)

    無宿主引用。

    [unowned self] 或[unowned(safe) self] 或[unowned(unsafe) self]


    weak

    弱引用,使得對象不會被持續占有

    總結

    以上是生活随笔為你收集整理的初探swift语言的学习笔记七(swift 的关健词)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。