import UIKit
//TODO:----------第一讲 (String 字符串
let tempValue = 34
var str = String();
str = "Hello World \(String(tempValue))"
//验证是否为空
if str.isEmpty
{
println("str is nil")
}
var count = countElements(str) //字符串长度
var greetiong = str.capitalizedString //capitalizedString
var upper = str.uppercaseString //转换成大写
//TODO:----------第二讲 (tuple 元组)
/*
将多个值合并为单个值,元组内的值可以是任意类型
*/
let tupleValueCode : Int = 404
let tupleValueString : String = "失败"
let httpError = (tupleValueCode , tupleValueString);
println("the status is \(httpError.0)")
println("the message is \(httpError.1) ")
println("the httpError is \(httpError)")
let httpStatus = (statusCode:200,statusMessage:"成功")
println("the status is \(httpStatus.statusCode)")
println("the message is \(httpStatus.statusMessage) ")
println("the httpError is \(httpStatus)")
//TODO:----------第三讲 (Optional 可选类型)
/*
它有值但是我们不确定
没有任何值
*/
let tempStr = "123"
let optionalStr : Int? = tempStr.toInt(); //字符串可能是整形数据
if optionalStr == nil{
println("tempStr = \(tempStr) 不是 整形")
}else{
println("tempStr = \(optionalStr!) 是 整形");
}
//TODO:----------第四讲 (Array 数组)
//初始化
var list1 = ["value1","value2","value3"];
//数组追加
list1.append("value4");
list1+=["value5"];
//数组长度
println("list1 is count : \(list1.count)")
//数组插入
list1.insert("value0", atIndex: 0);
//数组删除
list1.removeAtIndex(0);
println("list1 is content : \(list1)");
//数组遍历
for valueKey in list1 //(1)
{
println("\(valueKey)")
}
for (index , value) in enumerate(list1){ //(2)
println("\(index + 1):\(value)");
}
//TODO:----------第五讲 (Dictionary 字典)
//初始化
var dic :Dictionary<String,String> = ["key1":"value1","key2":"value2"]
//字典追加数据
dic["key3"] = "value3"
println("\(dic)")
//字典删除数据
if let removeValue = dic.removeValueForKey("key1"){
println("remove is : \(removeValue)")
}
println("dic content is : \(dic)")
//字典长度
println("dic is count : \(dic.count)")
//字典便利
for (dicKey, dicValue) in dic//(1)利用元素(同时遍历key和value)
{
println("key is content: \(dicKey)");
println("value is content : \(dicValue)");
}
for dicKey in dic.keys //(2)遍历key
{
println("key is content : \(dicKey)");
}
for dicValue in dic.values //(3)遍历value
{
println("value is content : \(dicValue)");
}
let keys = Array(dic.keys); //(5)获取所有Key
let values = Array(dic.values); //(6)获取所有value
//TODO:----------第六讲 (控制语句)
//分支语句
if 3 > 4{
} else if 3 == 4{
} else {
}
/* Switch(1) */
let switchTest: Character = "e"
switch switchTest{
case "a","b":
println(" \"a\",\"b\" is to \(switchTest)")
case "c","e":
println(" \"c\",\"e\" is to \(switchTest)")
default:
println("default is to \(switchTest)")
}
/* Switch(2) */
let switchTestCount = 3_000
let switchTestStr = " stars in the miky way"
var naturalCount:String
switch switchTestCount {
case 0:
naturalCount = "no"
case 1...3:
naturalCount = "a few"
case 4...9:
naturalCount = "seeral"
case 10...99:
naturalCount = "hundreds of"
case 100...999_999:
naturalCount = "thousands of"
default:
naturalCount = "default"
}
println("There are \(naturalCount + switchTestStr)")
//循环语句
/*while 水仙花数 */
var i = 100
var r = 0 //计算百位数
var s = 0 //计算十位数
var t = 0 //计算个位数
while i < 1000 {
r = i / 100
s = (i - r * 100 ) / 10
t = i - r * 100 - s * 10
if ( i == r * r * r + s * s * s + t * t * t) {
println("i = \(i)");
}
i++
}
/* do...while 水仙花数 */
i = 100
r = 0 //计算百位数
s = 0 //计算十位数
t = 0 //计算个位数
do {
r = i / 100
s = (i - r * 100 ) / 10
t = i - r * 100 - s * 10
if ( i == r * r * r + s * s * s + t * t * t) {
println("i = \(i)");
}
i++
}while i < 1000
/* for */
var forCount = 8
var rr = 0
var ss = 0
for var index = 0 ; index < forCount ; index++ {
rr = index * index
ss = rr * index
println("》》》》》》》》》》》》》》》》》》")
println("整数为 : \(index)")
println("对应平方和 : \(rr)")
println("对应立方和 : \(rr)")
}