swift(二)
来源:互联网 发布:歌曲软件下载大全 编辑:程序博客网 时间:2024/06/05 18:26
记录下 今天学的东西。。看到闭包了。。看不下去了。。
let strValue1:String? = "abcd"//声明了一个Optional类型值,可能包含一个String值,也可能什么都不包含 等同于 var strValue1: Optional<String>
let hasValue = strValue1?.characters.count
print("hasValue:\(hasValue)")//声明一个可选类型
let strValue2:String! = "def"//隐式拆包
let hasValue2 = strValue2.characters.count
print("hasValue:\(hasValue2)")
//使用强制拆包需要确定里面一定是非nil的值
var count :Int?
count = 100
if count !=nil {
print("count is\(String(count))")
}
//可选绑定
let possibleCount ="abc"
iflet actualNumber = Int(possibleCount) {
print("\(possibleCount) value of\(actualNumber)")
}else {
print("no value")
}
//as类型转换 is类型检查
//断言
// let age = -3
// assert(age >= 0, "This is assert")//会打印this is assert会崩
//创建一个集合
var setB =Set<String>()
setB.insert("abc")
setB.insert("def")
setB.insert("123")
print("\(setB)")
for jihein setB.sort() {
print("\(jihe)")
}
//字典
var airports: [String:String] = ["111":"a","222":"b"]
print("airports :\(airports.count)");
iflet oldValue = airports.updateValue("123456", forKey:"11") {
print("The old Value\(oldValue)")
}
for (airportCode, airportName)in airports {
print("\(airportName):\(airportCode)")
}
//swift中的fallthrough 使代码块执行继续连接到下一个case的执行代码和c的switch相同
//带标签的语句
//label name: while condition { statements }
//eg: gameLoop: while square != finalSquare {}
//使用guard执行取决于布尔值 一个guard里面总代有一个else
func greet(person: [String:String]) {
guardlet name = person["name"]else {
return
}
print("Hello\(name)")
guardlet location = person["location"]else {
print("I hope the weather is nice near you")
return
}
print("I hope the weather is nice in\(location).")
}
greet(["name":"John"])
- swift (二)
- swift(二)
- Swift(二)
- 【Swift初见】Swift数组(二)
- Swift--基本数据类型(二)
- Swift 基础语法二
- Swift基础学习二
- Swift初体验(二)
- Swift--数组(二)
- Swift学习二
- Swift(二)
- swift之GCD(二)
- Swift基础知识(二)
- swift学习笔记(二)
- Swift入门(二)
- 自学Swift(二)
- swift学习笔记二
- Swift教程(二)
- android webview加载html5乱码解决
- Spring配置文件 装配Bean
- 使用VISIO远程服务器上的ORACLE数据库,反向生成数据库实体关系图
- ubuntu14.10上编译Android5.0.2源码
- 一个很详细的web.xml讲解
- swift(二)
- android AsyncTask介绍
- Magento编译模式 - Magento Compiler Mode
- windows git 环境搭建
- Maximum Subarray Difference Solution
- java语言中的enum类型使用介绍
- 作業系統之前的程式 for stm32f4discovery (0) - 打造 linux 開發環境
- error: RPC failed; result=18, HTTP code = 200 gitlab 百兆以上库下载报错
- 写作的意境