Swift3.0 基礎語法

Swift特色

  • 蘋果宣稱Swift的特點是:快速、現(xiàn)代、安全、互動
  • Swift中取消了預編譯指令包括宏
  • 可以使用現(xiàn)有的CocoaCocoa Touch 框架
  • Swift取消了Objective-C的指針 及其他不安全訪問的使用
  • 舍棄Objective-C早期應用的smalltalk的語法,全面改為點語法
  • Swift被形容為“沒有CObjective-C”(Objective-C without the C)
  • 提供類似Java的命名空間、泛型、運算對象重載
  • Swift3.0對Foundation框架做了重大調整
    • 去除NS前綴
    • 將絕大部分class轉換為struct

變量與常量

  • 變量:使用var聲明
var str: String = "浮生若夢"
str = "sl"
  • 常量:使用let聲明
let count: Int = 10

基本數(shù)據(jù)類型

  • 整形
    • 有符號整形:Int,Int8,Int16,Int32,Int64
    • 無符號整形:UInt,UInt8,UInt16,UInt32,UInt64
  • 浮點型
    • Float:32位浮點數(shù)類型
    • Double:64位浮點數(shù)類型
  • 布爾型:Bool (flase or true)

字符串

var str: String = ""

// 1. 判斷字符串是否為空
if str.isEmpty {
    print("空字符串")
}

// 2. 獲取字符數(shù)量:string.characters.count
let str1 = "baidu.com"
print("\(str1.characters.count)個字符")

// 3.檢查字符串是否有特定前綴/后綴:hasPrefix/hasSuffix
if str1.hasPrefix("baidu") {
    print("百度")
}
if str1.hasSuffix("com") {
    print("hello")
}

// 4.可以用“\()”在字符串里包裹變量,常量
let name = "浮生"
print("歡迎\(name)")

// 5.字符串的大寫/小寫/首字母大寫
let msg = "Welecome to chengdu"
print(msg.uppercased())
print(msg.lowercased())
print(msg.capitalized)

字符:Character

let chr: Character = "@"

元組

let tuple = (name:"me",age:23)
print("\(tuple.name)") //me
print(tuple.age) //23
print(tuple.0) //me
print(tuple.1) //23
print(tuple) //("me",23)

Optional 可選項

  • ?定義可選項
    !對可選項解包

  • 要么有值,要么為nil

  • 參與計算前必須先解包

  • 常量可選項需要賦初始值

  • 變量定義可選項默認初始值是nil

  • ??操作符

// 如果可選值為空,使用`??`后面的值代替
var name: String? = nil
print((name ?? "浮生若夢")) //浮生若夢
name = "小霸王"
print((name ?? "浮生若夢")) //小霸王
  • 可選解包 if let/var 連用
var name: String? = nil
if let n = name { //name=nil 不會打印
    print(n)
} else {
    print("name為nil") //name為nil
}
name = "小霸王"
if let n = name {
    print(n) //小霸王
}
  • guard let可以減少if let 分支
      let name: String? = "浮生"
        
        guard let name = name else {
            print("name為nil")
            return
        }
        //執(zhí)行至此,name一定有值
        print(name) // 浮生

Switch

  • 可以對任意類型的值進行分支,不在局限為整數(shù)
  • 分支里一般不需要寫break
  • 多值判斷使用,分隔條件
  • 所有的分支都至少需要一條語句,如果什么都不做,就寫break
// 1. Swift中不需要在case塊中顯示地使用break跳出switch。如果想要實現(xiàn)C風格的落入特性,可以給需要的case分支插入fallthrough語句
let fruit = "apple"
switch fruit{
case "apple":
    print("good")
    fallthrough
case "banana","orange":
    print("great")
default:
    print("bad")
}

// 2. case分支還可以進行區(qū)間匹配
let age = 5
switch age {
case 0...11:
    print("小伙子")
case 12...30:
    print("年輕人")
default:
    print("大叔")
}

// 3.使用元組匹配(判斷屬于哪個象限)
let point = (2,2)
switch point {
case (0,0):
    print("坐標在原點")
case (_,0):
    print("坐標在x軸上")
case (0,_):
    print("坐標在y軸上")
case (-3...3, -3...3):
    print("坐標在長寬為6的正方形內(nèi)")
default:
    print("在什么地方")
}

// 4.case中還可以使用where關鍵字來做額外的判斷條件
let point = CGPoint(x: 10, y: 10)
switch point {
case let p where p.x == 0 && p.y == 0:
    print("原點")
case let p where p.x == 0:
    print("Y軸")
case let p where p.y == 0:
    print("X軸")
case let p where abs(p.x) == abs(p.y):
    print("對角線")
default:
    print("其他")
}

for循環(huán)

  • Swift3.0 去除傳統(tǒng)形式for循環(huán)語句
// for-in循環(huán)
// 變量i在[0,3)循環(huán)
for i in 0..<3 {
    print(i)
} //0,1,2

// 變量i在[0,3]循環(huán)
for i in 0...3 {
    print(i)
} //0,1,2,3

// 倒序遍歷
for i in (0...3).reversed() {
    print(i)
} //3,2,1,0

// for-each循環(huán)
(1...3).forEach {
    print($0)
} //1,2,3

while循環(huán)

var i = 0
while i<100 {
    i+=1
}
 
var i = 0
repeat{
    i+=1
}while i<100

類擴展

Swift語言的類擴展是一個強大的工具,我們可以通過類擴展完成如下事情:

1. 給已有的類添加計算屬性和計算靜態(tài)屬性
2. 定義新的實例方法和類方法
3. 提供新的構造器
4. 定義下標腳本
5. 是一個已有的類型符合某個協(xié)議
注意:擴展只能添加新的計算型屬性,不能添加存儲型屬性,也不能添加新的屬性監(jiān)視器
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

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

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