Swift在2014開發(fā)者大會上提出后沟饥。幾乎每年都會更新維護摄凡,現在已經更新到2.0.
第一次使用swift炉峰。邊學swift当悔,邊做一些筆記分析傅瞻。希望能快速入門這門新語言吧~
Swift語言特點
作為一項蘋果獨立發(fā)布的支持型開發(fā)語言踢代,Swift語言的語法內容混合了Objective-C、JS嗅骄、Python胳挎,語法簡單、使用方便溺森、易學慕爬,大大降低了開發(fā)者入門的門檻。同時swift語言可以與Objective-C混合使用屏积,對于用慣了高難度Objective C語言的程序猿來說医窿,學會這個,更不在話下炊林!
使用條件
Xcode版本>=6.0
Mac系統版本>=10.9.3
1.常量和變量
let 和 val 聲明一個常量和變量姥卢。
2.OC和Swift混編,頭文件導入
當在swift系統文件中創(chuàng)建object文件時渣聚,需要導入object-C文件才可以使用oc類中的方法独榴。
當創(chuàng)建的時候,會提醒是否創(chuàng)建橋接奕枝。選擇時棺榔,即可
如圖:將oc頭文件放進去。
swift與OC語法對比分析
3.swift 控件的創(chuàng)建
// 一個完整的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í)行點擊事件
func ClickBtnSelect() {
NSLog("點擊了按鈕")
print("點擊了按鈕")
}
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
}()
// 調用部分
self.view .addSubview(LazyBtn)
綜上:Swift真是一個自上而下倍权,簡潔掷豺,易學的代碼。薄声。
開始試著找一款APP模仿著學習吧当船。
我模仿的是維尼的小熊#小日子#,因為搜到了Swift源碼默辨。
5.訪問控制的認識
蘋果在發(fā)布了Xcode 6 Bate 4后為Swift添加了新的特性–訪問控制(Access Control) 并為此寫了文檔“The Swift Programming Language”
具體內容來源
什么是訪問控制:
訪問控制可以限定你在源文件或模塊中訪問代碼的級別德频,限定哪些代碼允許被訪問,哪些代碼不允許被訪問缩幸。
適用范圍:
整個項目壹置。
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.
可以明確的給類、結構體表谊、枚舉钞护、設置訪問級別,也可以給屬性爆办、函數难咕、初始化方法、基本類型、下標索引等設置訪問級別余佃。協議也可以被限定在一定的范圍內使用暮刃,包括協議里的全局常量、變量和函數爆土。
名詞解釋:模塊和源文件
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中的訪問控制模型基于模塊和源文件這兩個概念椭懊。
模塊指的是Framework或App bundle。在Swift中步势,可以用import關鍵字引入自己的工程氧猬。
在Swift中,Frameword或App bundle被作為模塊處理坏瘩。如果你是為了實現某個通用的功能狂窑,或者是為了封裝一些常用方法而將代碼打包成Framework,這個Framework在Swift中就被稱為模塊桑腮。不論它被引入到某個App工程或者其他的Framework泉哈,它里面的一切(屬性、函數等)都屬于這個模塊破讨。
源文件指的是Swift中的Swift File丛晦,就是編寫Swift代碼的文件,它通常屬于一個模塊提陶。通常一個源文件包含一個類烫沙,在類中又包含函數、屬性等類型隙笆。
訪問級別
<font color=red>內容</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提供了三種不同的訪問級別锌蓄。這些訪問級別相對于源文件中定義的實體,同時也相對于這些源文件所屬的模塊撑柔。
Public:可以訪問自己模塊或應用中源文件里的任何實體瘸爽,別人也可以訪問引入該模塊中源文件里的所有實體。通常情況下铅忿,某個接口或Framework是可以被任何人使用時剪决,你可以將其設置為public級別。
Internal:可以訪問自己模塊或應用中源文件里的任何實體檀训,但是別人不能訪問該模塊中源文件里的實體柑潦。通常情況下,某個接口或Framework作為內部結構使用時峻凫,你可以將其設置為internal級別渗鬼。
Private:只能在當前源文件中使用的實體,稱為私有實體荧琼。使用private級別譬胎,可以用作隱藏某些功能的實現細節(jié)肛循。
Public為最高級訪問級別,Private為最低級訪問級別银择。
-
Swift語言的改變
“確保轉換”和“可失敗轉換”的概念現在被分為兩個操作符±巯希可失敗轉換現在使用as!運算符浩考,這個!感嘆號可以讓代碼的讀者更清晰的明白本次轉換可能失敗并觸發(fā)一個運行時錯誤被盈∥瞿酰“as”操作符會保持向上轉換(比如“someDerivedValue轉換為Base”)或者類型標注(“0 轉換為Int8”),它保證了轉換不會失敗只怎。
結構體和類構造器中的let不可變屬性現在被規(guī)范為更加標準的通用模型:lets類型初始化后將永不會被改變或重新賦值袜瞬。以前的實現是,可以在構造器中任意修改身堡,而現在它們只允許被初始化和提供值操作邓尤。如果一個屬性在聲明時已經賦值,那么它會被所有的構造器認為已經含有初始值贴谎。
從橋接Objective-C類 (NSString/NSArray/NSDictionary)到它Swift中值類型的隱式轉化被移除汞扎。這將是Swift的類型系統更加簡單和可預測。這意味著:
import Foundation
func log(s: String) { println(x) }
let ns: NSString =
"some NSString"
// Okay
log(ns)
// 錯誤
// "'NSString' 不能轉換為 'String'"
為了完成橋接轉換擅这,需要用顯式轉化符標注:
log(ns as String)
// succeeds
從Swift類型到Objective-C類型的橋接隱式轉換依然被允許澈魄,比如:
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