swift(二)
2015-10-22 17:37
337 查看
记录下 今天学的东西。。看到闭包了。。看不下去了。。
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"
if
let 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 jihe
in setB.sort() {
print("\(jihe)")
}
//字典
var airports: [String:
String] = ["111":"a","222":"b"]
print("airports :\(airports.count)");
if
let 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]) {
guard
let name = person["name"]
else {
return
}
print("Hello
\(name)")
guard
let 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"])
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"
if
let 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 jihe
in setB.sort() {
print("\(jihe)")
}
//字典
var airports: [String:
String] = ["111":"a","222":"b"]
print("airports :\(airports.count)");
if
let 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]) {
guard
let name = person["name"]
else {
return
}
print("Hello
\(name)")
guard
let 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"])
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- Apple Swift学习教程
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- Swift中实现点击、双击、捏、旋转、拖动、划动、长按手势的类和方法介绍
- Swift中定义二维数组的方法及遍历方法示例
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?
- Android、iOS和Windows Phone中的推送技术详解
- 简单分析Swift语言的一些基本特征
- Swift与C语言指针结合使用实例