一、數據類型
1.基本數據類型
let a:Int = 12 //聲明整形常量a , let 代表常量曾撤,Int代表整形
var b:Float = 10.2 //聲明浮點型變量b棠枉,var代表變量,Float代表浮點型颠蕴,初值為10.2
let str = "helloWorld"
str.characters.count //計算字符串長度
str.lengthOfBytesUsingEncoding(NSUTF8StringEncoding) //計算字符串長度
注:字符串相等判斷: "=="泣刹,字符串拼接判斷:“+”
str.uppercaseString // 首字母大寫
str.lowercaseString // 首字母小寫
// 格式化字符串(兩種方法)
let str2 = String(format: "%02d:%02d", 1,2); //方法1
let str3 = String(format: "%02d-%02d", arguments: [1,2]) // 方法2
2.數組 Array (跟OC里面的數組一樣可以相互轉換)
a.不可變數組
let array1 = ["A", "2", "3", "4"] //定義不可變數組array1
// initWith/init在swift里面全部轉換為(),代表初始化
let array2 = [String]() //初始化定義不可變數組array2
// 訪問數組元素個數 .count
// 數組遍歷 for-in(明確指出數組的類型)
for temp in array1 as [String]{
print(temp.characters.count)
}
// 元組遍歷
for(index, value) in array1.enumerate()
{
print("index = \(index), value = \(value)") // \(參數)為占位符
}
// b.可變數組
var mutableArray = [String]() // 定義可變數組(類型為NSString)
mutableArray.append("hello") // 添加元素
//mutableArray.append(23) //類型不匹配
mutableArray.removeAll() //移除數組元素
// mutableArray.removeFirst(<#T##n: Int##Int#>) 從第一個元素開始移除, 直到n結束
3. 字典 Dictionary
let dict = ["key1":"value1", "key2":"value2", "key3":"value3"] //定義不可變字典
// 通過key值訪問value
print(dict["key1"])
// for-in遍歷
for (key,value) in dict // 遍歷字典里面所有的key和value
{
print("key = \(key), value = \(value)")
}
var dict3 = ["key":"value"]
for (tempkey, tempValue) in dict{
// 如果key存在, 則是一個更新鍵值對操作, 否則是一個增加鍵值對操作
dict3[tempkey] = tempValue
}
4. ? 與 犀被!的應用
// 如果一個值可能為nil, 那么這個值就是可選類型,用椅您?標識
// optional包圍的就是一個可選類型
// 可選類型不能直接使用, 必須進行強制解包,!強制解包, 對nil強制解包會造成崩潰
var a1:String?
a1 = "12345"
// !!!!!unexpectedly found nil while unwrapping an Optional value 對一個空的可選類型進行強制解包
// (a1 ?? "234") 對一個空的可選類型進行強制解包, 如果可選類型為nil, 則給他一個默認值
print((a1 ?? "234").characters.count)
二. 分支語句
// if 條件語句()可以省略, {}不能省略
// if 沒有非0即真的概念, 只有true 和 false 的兩種情況
let tempValue = 10
if tempValue > 5{
print("tempValue > 5")
}
1. 可選類型的條件分支
let str:String? = "hello"
a.if-let
對可選類型的判斷, 如果可選類型為空, 則不執(zhí)行代碼塊, 如果不為空, 則用tempStr來接收此刻這個可選類型解包后的值
// 只針對可選類型 ?
if let tempStr = str{
print(tempStr)
}
if str !=nil // 安全處理,防止str為空導致crash
{ str! }
b.if-let-where
跟if-let 相似, where是對前面定義的這個局部變量在做一層判斷
if let tempStr = str where tempStr.characters.count > 2{
print("tempStr.length = \(tempStr.characters.count)")
}
c. guard-let-else
如果可選類型str為nil,則執(zhí)行code代碼塊, 最后一定要return, 如果不為nil, 則強制解包后的值賦值給tempStr, 這樣在{}外面就可以使用tempStr
guard let tempStr = str else
{
//code
return
}
tempStr...
2. switch分支語句
switch 不局限判斷整形, 可以為浮點型, 也可以是字符串...
switch 判斷后面的小括號可以省略, 大括號不能省略
case 后面至少要有一條執(zhí)行語句H跖小=缶凇!昌腰!并且case后面的大括號可以省略, break可以不寫, 不會造成貫穿
default一定要寫, 并且只能寫在最后
let f = 3.2
switch f
{
case 3.0:
print("===3.0")
case 3.1:
print("===3.1")
case 3.2:
print("3.2")
default:
print("know")
}
3. for...in 遍歷
for (var i = 0; i < 5; i++){ swift2.2的時候被廢棄
}
for var i = 0;i < 5; i += 1{
print(i)
}
for i in 0..<5 { // 0..<5-----[0,5) 0...5-----[0,5]
print("i = \(i)")
}
三. 枚舉
a.枚舉值可以關聯(lián)浮點, 字符串, 沒有默認的關聯(lián)值开伏,關聯(lián)如果是Int, 會默認的遞增上去, 如果不是Int類型, 必須每個枚舉值都關聯(lián)上
enum Month:CGFloat{ // 定義枚舉
case January = 10.1
case February = 10.2
case March = 10.3
case April = 10.4
}
b.如果明確指出一個變量/常量是屬于哪種枚舉類型的話, 可以直接.枚舉值, 否則就 枚舉類型.枚舉值
/*
let month:Month = .February // 定義month枚舉常量,值為February
var month1 = Month.January //定義month1枚舉變量
month1 = .February
*/
switch month{
case .January:
print("hashValue = \(month.hashValue), rawValue = \(month.rawValue)")
print(month)
case .February:
print("hashValue = \(month.hashValue), rawValue = \(month.rawValue)")
print(month)
case .March:
print("hashValue = \(month.hashValue), rawValue = \(month.rawValue)")
print(month)
case .April:
print("hashValue = \(month.hashValue), rawValue = \(month.rawValue)")
print(month)
}
注:hashValue 的值為位置, rawValue為關聯(lián)的值