文章是根據(jù)The Swift Programming Language 來總結(jié)的朋凉,今天就學(xué)習(xí)一下最基礎(chǔ)的控制符蒲赂,本文總結(jié)的是
if
阱冶、switch
、for-in
滥嘴、for
木蹬、while
、repeat-while
** 首先注意的是:條件語句括號(hào)
()
可以省略若皱,跟其他編程語言有很大的差別镊叁,但是語句體的大括號(hào){}
是必須的,下面會(huì)有demo展示 **
if
** 基本語法 **
OC:
Bool flag = YES;
if (flag) {
// TODO
}
Swift:
let flag = true
if flag {
// TODO
}
if (flag) {
// TODO
}
** 區(qū)別 **
在Swift中走触,條件必須是一個(gè)布爾表達(dá)式晦譬,也就是說不是true就是false
但是在OC中,什么表達(dá)式都可以互广,前提是條件語句成立敛腌,甚至是一個(gè)對(duì)象
OC:
NSString *str = @"條件";
if (str) {
// TODO
}
NSInteger age = 10;
if (age) {
// TODO
}
但是在Swift不行,一下是錯(cuò)誤寫法,會(huì)報(bào)出:
Type 'String' does not coonform to protocol 'BooleanType'
Type 'Int' does not coonform to protocol 'BooleanType'
Swift:
var name = "條件"
if name {
// TODO
}
let age = 10
if age {
// TODO
}
** 正確的寫法是: **
Swift:
var name : String? = "條件"
if let newName = name {
// TODO
print(newName); // 打印條件
}
let age = 10
if age > 5 {
// TODO
}
這里可能涉及一點(diǎn)其他知識(shí)惫皱,就是條件語句使用字符串像樊,不敢保證是不為nil的,所以需要用let處理字符串確實(shí)旅敷,就是字符串為nil凶硅,用到可選類型,就是在聲明變量類型后面加上一個(gè)問號(hào)
var name : String? = "條件"
這句就是表示,name可以有值扫皱,也可以為nil足绅,是可選類型
搭配if來用
if let newName = name {
// TODO
print(newName); // 打印條件
}
如果name是nil,為false韩脑,那么就不會(huì)條件代碼塊氢妈,當(dāng)name不為nil的時(shí)候,賦值給newName段多,條件成立首量,為true,執(zhí)行大括號(hào)的代碼塊。當(dāng)然也可以這樣寫
if let newName: String? = "字符串" {
// TODO
print(newName); // 打印條件
}
switch
switch沒什么值得講的加缘,用發(fā)跟OC一樣鸭叙,但是條件類型就不是一樣了,Java6以上是支持字符串的拣宏,Swift也是一樣沈贝,格式是:
格式: switch(需要匹配的值) case 匹配的值: 需要執(zhí)行的語句 break;而且條件** 支持任意類型的數(shù)據(jù)以及各種比較操作 **
OC與Swift的區(qū)別:
- OC可以不寫default,default位置可以隨便寫
OC:
NSInteger age = 10;
switch (age) {
case 1:
{
break;
}
case 2:
{
break;
}
}
- Swift一定要寫default勋乾,default位置只能在最后
Swift:
let age = 10
switch age {
case 1:
print("ahah")
default:
print("haha")
}
- OC不能判斷對(duì)象宋下,必須是整數(shù)
OC:
NSNumber *age = @10;
switch (age) {
case @1:
{
break;
}
case @2:
{
break;
}
}
- Swift可以判斷對(duì)象
Swift:
var name = "daisuke"
switch name {
case "daisuke":
print("ahah")
case "alex":
print("ahah")
default:
print("haha")
}
- OC可以穿透
OC:
NSInteger age = 10;
switch (age) {
case 1:
case 2:
case 3:
{
break;
}
}
- Swift不可以穿透,可以不寫break
Swift:
let age = 10
switch age {
case 1: // 相當(dāng)于 if
print("ahah")
case 2: // 相當(dāng)于 else if
print("ahah")
case 3: // 相當(dāng)于 else if
print("ahah")
default:
print("haha")
}
// 不可以這樣寫辑莫,因?yàn)椴荒艽┩?Swift:
let age = 10
switch age {
case 1:
case 2:
case 3:
print("ahah")
default:
print("haha")
}
// 如果一定要跳過某個(gè)條件的話学歧,可以這樣寫,但是OC絕對(duì)不可以
Swift:
let age = 10
switch age {
case 1 , 2:
case 3:
print("ahah")
default:
print("haha")
}
- OC在case中定義變量或者其他語句各吨,一定要花括號(hào)
{}
枝笨,不然作用域混亂
OC:
NSInteger age = 10;
switch (age) {
case 1:
{
NSInteger height = 170;
break;
}
case 2:
case 3:
{
break;
}
}
- Swift在case中定義變量或者其他語句,不需要花括號(hào)
Swift:
let age = 10
switch age {
case 1: // 相當(dāng)于 if
var name = "daisuke"
var height = 170
print("ahah")
case 2: // 相當(dāng)于 else if
print("ahah")
case 3: // 相當(dāng)于 else if
print("ahah")
default:
print("haha")
}
for
在OC中的兩種for循環(huán)方式
格式: for (初始化表達(dá)式;循環(huán)保持條件;循環(huán)后表達(dá)式) {需要執(zhí)行的語句}
for (int index = 0; index < 10; index++) {
// TODO
}
// 其中括號(hào)里面的條件`樣式`想怎么搞就怎么搞
在Swift的for循環(huán)方式
1、for后面的圓括號(hào)可以省略
2揭蜒、只能是bool類型作為條件語句
3伺帘、如果只有一條執(zhí)行代碼,for后面的花括號(hào)不可以省略
4忌锯、for后面的三個(gè)參數(shù)都可以省略
根據(jù)下標(biāo)實(shí)現(xiàn)循環(huán)
var sum:Int = 0;
for var index = 0; index<10; index++
{
sum += index
}
for in
格式: for (接收參數(shù) in 取出的參數(shù)) {需要執(zhí)行的語句}
for in含義: 從(in)取出什么給什么, 直到取完為止
for in 一般用于遍歷區(qū)間或者集合
NSArray *arrary = @[@{@"key":@"value"}, @{@"key1":@"value1"}];
for (NSDictionary *dict in arrary) {
NSLog(@"%@", dict[@"key"]);
}
var sum:Int = 0;
for var index in 1...10
{
// 1...10,表示1到10這個(gè)十個(gè)數(shù),會(huì)將區(qū)間的值依次賦值給index
sum += index
}
var dict = ["name":"daisuke", "age":10]
for (key, value) in dict
{
print("\(key) = \(value)")
}
while
- OC格式:while(循環(huán)保持條件){需要執(zhí)行的語句}
int index = 0;
int sum = 0;
while(index < 10)
{
index++;
sum += index;
}
int index = 0;
int sum = 0;
while(index < 10)
sum += index++;
// 只有一條執(zhí)行語句的時(shí)候伪嫁。花括號(hào)可以省略
- Swift格式:while后的圓括號(hào)可以省略,只能以bool作為條件語句,如果只有一條指令while后面的大括號(hào)不可以省略
var index = 0
var sum = 0
while index < 10
{
index++
sum += index
}
var index = 0
var sum = 0
while index < 10
{
sum += index++
}
// 只有一條執(zhí)行語句的時(shí)候偶垮≌趴龋花括號(hào)不可以省略
repeat while
- OC中do while循環(huán),格式:do while(循環(huán)保持條件) {需要執(zhí)行的語句}
int index = 0;
int sum = 0;
do {
sum += index;
index++;
}
while(index < 10);
- 在Swift2.0之后改為 repeat while, do用于捕捉異常似舵。格式:while后面的圓括號(hào)可以省略脚猾,只能bool作為條件語句,只有一條語句的時(shí)候砚哗,do后面的花括號(hào)不能省略
var index = 0
var sum = 0
repeat{
sum += index
index++
}
while index < 10
var index = 0
var sum = 0
repeat{
sum += index++
}
while index < 10
想了解更多龙助,歡迎來我的網(wǎng)站daisuke