初始Swift


Swift在2014開發(fā)者大會(huì)上提出后。幾乎每年都會(huì)更新維護(hù),現(xiàn)在已經(jīng)更新到2.0.
第一次使用swift。邊學(xué)swift,邊做一些筆記分析。希望能快速入門這門新語言吧~

Swift語言特點(diǎn)

作為一項(xiàng)蘋果獨(dú)立發(fā)布的支持型開發(fā)語言,Swift語言的語法內(nèi)容混合了Objective-C、JS、Python,語法簡(jiǎn)單、使用方便、易學(xué),大大降低了開發(fā)者入門的門檻。同時(shí)swift語言可以與Objective-C混合使用,對(duì)于用慣了高難度Objective C語言的程序猿來說,學(xué)會(huì)這個(gè),更不在話下!


使用條件
Xcode版本>=6.0
Mac系統(tǒng)版本>=10.9.3

1.常量和變量

let 和 val 聲明一個(gè)常量和變量。

2.OC和Swift混編,頭文件導(dǎo)入
當(dāng)在swift系統(tǒng)文件中創(chuàng)建object文件時(shí),需要導(dǎo)入object-C文件才可以使用oc類中的方法。
當(dāng)創(chuàng)建的時(shí)候,會(huì)提醒是否創(chuàng)建橋接。選擇時(shí),即可
如圖:將oc頭文件放進(jìn)去。


橋接Swift.png

swift與OC語法對(duì)比分析
3.swift 控件的創(chuàng)建

// 一個(gè)完整的btn
           let SwiftBtn =  UIButton ()
        SwiftBtn.frame = CGRectMake(0, 0, 100 ,100)
        SwiftBtn.setTitle("Swift大法好", forState: UIControlState.Normal)
        SwiftBtn.setTitleColor(UIColor .blackColor(), forState: UIControlState.Normal)
        SwiftBtn.backgroundColor = UIColor .redColor()
        SwiftBtn .addTarget(self, action: #selector(ViewController.ClickBtnSelect), forControlEvents: UIControlEvents.TouchUpInside)
        self.view .addSubview(SwiftBtn)

// 執(zhí)行點(diǎn)擊事件
 func ClickBtnSelect()  {
        NSLog("點(diǎn)擊了按鈕")
        print("點(diǎn)擊了按鈕")
        
    }

4.控件的懶加載

  // 懶加載
    let LazyBtn = UIButton()
    LazyBtn.frame = CGRectMake(120, 0, 100, 100)
    LazyBtn.setTitle("Swift懶加載", forState: UIControlState.Normal)
    LazyBtn.setTitleColor(UIColor .blackColor(), forState: UIControlState.Normal)
    LazyBtn.backgroundColor = UIColor .redColor()
    return LazyBtn
    }()

 // 調(diào)用部分
  self.view .addSubview(LazyBtn)

綜上:Swift真是一個(gè)自上而下,簡(jiǎn)潔,易學(xué)的代碼。。

開始試著找一款A(yù)PP模仿著學(xué)習(xí)吧。
我模仿的是維尼的小熊#小日子#,因?yàn)樗训搅薙wift源碼。

5.訪問控制的認(rèn)識(shí)
蘋果在發(fā)布了Xcode 6 Bate 4后為Swift添加了新的特性–訪問控制(Access Control) 并為此寫了文檔“The Swift Programming Language
具體內(nèi)容來源
什么是訪問控制:
訪問控制可以限定你在源文件或模塊中訪問代碼的級(jí)別,限定哪些代碼允許被訪問,哪些代碼不允許被訪問。
適用范圍:
整個(gè)項(xiàng)目。

You can assign specific access levels to individual types (classes, structures, and enumerations), as well as to properties, methods, initializers, and subscripts belonging to those types. Protocols can be restricted to a certain context, as can global constants, variables, and functions.
可以明確的給類、結(jié)構(gòu)體、枚舉、設(shè)置訪問級(jí)別,也可以給屬性、函數(shù)、初始化方法、基本類型、下標(biāo)索引等設(shè)置訪問級(jí)別。協(xié)議也可以被限定在一定的范圍內(nèi)使用,包括協(xié)議里的全局常量、變量和函數(shù)。

名詞解釋:模塊和源文件

Modules and Source Files
Swift’s access control model is based on the concept of modules and source files.
A module is a single unit of code distribution—a framework or application that is built and shipped as a single unit and that can be imported by another module with Swift’simport
keyword.
Each build target (such as an app bundle or framework) in Xcode is treated as a separate module in Swift. If you group together aspects of your app’s code as a stand-alone framework—perhaps to encapsulate and reuse that code across multiple applications—then everything you define within that framework will be part of a separate module when it is imported and used within an app, or when it is used within another framework.
A source file is a single Swift source code file within a module (in effect, a single file within an app or framework). Although it is common to define individual types in separate source files, a single source file can contain definitions for multiple types, functions, and so on.
Swift中的訪問控制模型基于模塊和源文件這兩個(gè)概念。
模塊指的是Framework或App bundle。在Swift中,可以用import關(guān)鍵字引入自己的工程。
在Swift中,F(xiàn)rameword或App bundle被作為模塊處理。如果你是為了實(shí)現(xiàn)某個(gè)通用的功能,或者是為了封裝一些常用方法而將代碼打包成Framework,這個(gè)Framework在Swift中就被稱為模塊。不論它被引入到某個(gè)App工程或者其他的Framework,它里面的一切(屬性、函數(shù)等)都屬于這個(gè)模塊。
源文件指的是Swift中的Swift File,就是編寫Swift代碼的文件,它通常屬于一個(gè)模塊。通常一個(gè)源文件包含一個(gè)類,在類中又包含函數(shù)、屬性等類型。

訪問級(jí)別
<font color=red>內(nèi)容</font>
Access Levels
Swift provides three different access levels for entities within your code. These access levels are relative to the source file in which an entity is defined, and also relative to the module that source file belongs to.
Public access enables entities to be used within any source file from their defining module, and also in a source file from another module that imports the defining module. You typically use public access when specifying the public interface to a framework.
Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app’s or a framework’s internal structure.
Private access restricts the use of an entity to its own defining source file. Use private access to hide the implementation details of a specific piece of functionality.
Swift提供了三種不同的訪問級(jí)別。這些訪問級(jí)別相對(duì)于源文件中定義的實(shí)體,同時(shí)也相對(duì)于這些源文件所屬的模塊。
Public:可以訪問自己模塊或應(yīng)用中源文件里的任何實(shí)體,別人也可以訪問引入該模塊中源文件里的所有實(shí)體。通常情況下,某個(gè)接口或Framework是可以被任何人使用時(shí),你可以將其設(shè)置為public級(jí)別。
Internal:可以訪問自己模塊或應(yīng)用中源文件里的任何實(shí)體,但是別人不能訪問該模塊中源文件里的實(shí)體。通常情況下,某個(gè)接口或Framework作為內(nèi)部結(jié)構(gòu)使用時(shí),你可以將其設(shè)置為internal級(jí)別。
Private:只能在當(dāng)前源文件中使用的實(shí)體,稱為私有實(shí)體。使用private級(jí)別,可以用作隱藏某些功能的實(shí)現(xiàn)細(xì)節(jié)。
Public為最高級(jí)訪問級(jí)別,Private為最低級(jí)訪問級(jí)別。

  1. Swift語言的改變
    “確保轉(zhuǎn)換”和“可失敗轉(zhuǎn)換”的概念現(xiàn)在被分為兩個(gè)操作符??墒∞D(zhuǎn)換現(xiàn)在使用as!運(yùn)算符,這個(gè)!感嘆號(hào)可以讓代碼的讀者更清晰的明白本次轉(zhuǎn)換可能失敗并觸發(fā)一個(gè)運(yùn)行時(shí)錯(cuò)誤?!癮s”操作符會(huì)保持向上轉(zhuǎn)換(比如“someDerivedValue轉(zhuǎn)換為Base”)或者類型標(biāo)注(“0 轉(zhuǎn)換為Int8”),它保證了轉(zhuǎn)換不會(huì)失敗。
    結(jié)構(gòu)體和類構(gòu)造器中的let不可變屬性現(xiàn)在被規(guī)范為更加標(biāo)準(zhǔn)的通用模型:lets類型初始化后將永不會(huì)被改變或重新賦值。以前的實(shí)現(xiàn)是,可以在構(gòu)造器中任意修改,而現(xiàn)在它們只允許被初始化和提供值操作。如果一個(gè)屬性在聲明時(shí)已經(jīng)賦值,那么它會(huì)被所有的構(gòu)造器認(rèn)為已經(jīng)含有初始值。
    從橋接Objective-C類 (NSString/NSArray/NSDictionary)到它Swift中值類型的隱式轉(zhuǎn)化被移除。這將是Swift的類型系統(tǒng)更加簡(jiǎn)單和可預(yù)測(cè)。這意味著:
import Foundation  
func log(s: String) { println(x) }  
let ns: NSString = 
"some NSString"
 // Okay  
log(ns) 
// 錯(cuò)誤  
// "'NSString' 不能轉(zhuǎn)換為 'String'"
為了完成橋接轉(zhuǎn)換,需要用顯式轉(zhuǎn)化符標(biāo)注:
log(ns as String) 
// succeeds
從Swift類型到Objective-C類型的橋接隱式轉(zhuǎn)換依然被允許,比如:
func nsLog(ns: NSString) { println(ns) }  
let s: String = “some String”  
nsLog(s) 
// okay: implicit conversion from String to NSString is still permitted

6.自定義Tabber

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Chapters: Enumerations - Advanced Operators Excerpt From:...
    碧波浮沉閱讀 708評(píng)論 0 1
  • 一顆想去看世界的心從八月一直等到十一月,說走就走的沖勁不是時(shí)時(shí)都有。很慶幸選擇了杭州,遇見一座城,從一個(gè)故事開始。...
    望涼月閱讀 373評(píng)論 2 3
  • 一如既往,秋高氣爽 世界干凈的如同天堂 就連,卑微的生活都帶著使命 和歸宿 金黃金黃 瓦藍(lán)瓦藍(lán) 是不是要做點(diǎn)兒什么...
    西詩人生閱讀 363評(píng)論 0 2
  • 圣誕夜,念恩師。晚上加班加點(diǎn)終于趕完了新接手競(jìng)價(jià)賬戶的完善工作,盡管已經(jīng)是第N次搭賬戶了,可每一次的感受都不盡相同...
    墨墨漲知識(shí)閱讀 609評(píng)論 0 50
  • WebSocket(實(shí)現(xiàn)持久連接) http://www.runoob.com/html/html5-websoc...
    WMLJS閱讀 2,790評(píng)論 0 0

友情鏈接更多精彩內(nèi)容