swift 学习记录(四)

来源:互联网 发布:淘宝网店运营培训班 编辑:程序博客网 时间:2024/06/11 13:37
//练习一
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不能省略
0 0
原创粉丝点击