swift 學習記錄(四)
阿新 • • 發佈:2018-12-23
//練習一
var explicitFloat:Float = 50
print(explicitFloat)
//練習二
var implicitFloat = 20.0
print("Hello Jim ,I geive you \(implicitFloat) money")
//標記:在if 語句中,條件必須是一個布林表示式——這意味著像if score { ... } 這樣的程式碼將報錯,而不會隱形地
//與 0 做對比。
//可選型別
var optionalString: String?
print(optionalString == nil)//說明預設的是nil
var optionalString1: String? = ""
print(optionalString1 == nil)//說明""也是佔記憶體的,與nil不同
var optionalString2: String? = "nil"
print(optionalString2 == nil)//說明"nil"是個字串,與nil不同
var optionalString3: String? = "Hello World"
print(optionalString3 == nil)//說明optionalString3有值
//switch
//執行switch 中匹配到的子句之後,程式會退出switch 語句,並不會繼續向下執行,所以不需要在每個子句結尾
//寫break 。
let vegetable = "red pepper"
switch vegetable {
case "celery":
print("Add some raisins and make ants on a log.")
case "cucumber", "watercress":
print("That would make a good tea sandwich.")
case let x where x.hasSuffix("pepper"):
print("Is it a spicy \(x)?")
default:
print("Everything tastes good in soup.")
}//刪除default報錯:error: switch must be exhaustive, consider adding a default clause,default不能省略
var explicitFloat:Float = 50
print(explicitFloat)
//練習二
var implicitFloat = 20.0
print("Hello Jim ,I geive you \(implicitFloat) money")
//標記:在if 語句中,條件必須是一個布林表示式——這意味著像if score { ... } 這樣的程式碼將報錯,而不會隱形地
//與 0 做對比。
//可選型別
var optionalString: String?
print(optionalString == nil)//說明預設的是nil
var optionalString1: String? = ""
print(optionalString1 == nil)//說明""也是佔記憶體的,與nil不同
var optionalString2: String? = "nil"
print(optionalString2 == nil)//說明"nil"是個字串,與nil不同
var optionalString3: String? = "Hello World"
print(optionalString3 == nil)//說明optionalString3有值
//switch
//執行switch 中匹配到的子句之後,程式會退出switch 語句,並不會繼續向下執行,所以不需要在每個子句結尾
//寫break 。
let vegetable = "red pepper"
switch vegetable {
case "celery":
print("Add some raisins and make ants on a log.")
case "cucumber", "watercress":
print("That would make a good tea sandwich.")
case let x where x.hasSuffix("pepper"):
print("Is it a spicy \(x)?")
default:
print("Everything tastes good in soup.")
}//刪除default報錯:error: switch must be exhaustive, consider adding a default clause,default不能省略