swift学习笔记
//常量定义 let a = "常量" print(a,a) print("\(a)\n\(a)") //换行 双引号中的\n 其它常量或者变量需要加 \() //声明常量的时候,指定常量类型 let b:Int = 0 let d:Double = 10.0 let e:String = "sss" //swift中变量的声明 var av = "3" var bv = av + "pinjie" //字符串拼接 var cv = bv print(cv) //比较两个字符串是否相等,在swift中,指针的概念比较淡化,比较直接用等号 if(bv == cv){ print("xiang deng") } //判断字符串是否为空 if e.isEmpty { print("字符串e是空") }else{ print("e不是空的") } //获取字符串的长度 let length = cv.characters.count print("字符串cv的长度是",length) //swift中的类型,没有隐式转换,只有显式转换,强制转换 var vaa = Int(d) //double转化为int 不能把字符型转为int double print(vaa) print("我是\(vaa),\n你是\(cv)") //插入字符 var str_var = "xiaoming" str_var.insert("a", atIndex: str_var.startIndex.advancedBy(4)); print(str_var)
var arr:Array = ["aa",1,["aa",3.2]] for(var i = 0; i < arr.count; i++) { print(arr[i].className) } var int_arr = [Int32]() //增加元素 int_arr.append(3) int_arr.append(4) int_arr += [2,6,7,3] //在特定某地增加元素 int_arr.insert(12, atIndex: 1) print(int_arr) //移除 int_arr.removeAtIndex(1) //int_arr.removeAll() print(int_arr) //开区间... for var i in 0...arr.count-1 { print(arr[i]) } //字典 //不可变 let dic:Dictionary<String,Int> = ["c":6,"a":10,"b":20] print(dic) //可变 var var_dic = Dictionary<String,Int>() var_dic["age"] = 90 var_dic["age"] = 23 print(var_dic["age"]) print(var_dic) var vdic:Dictionary = ["age":20,"name":"chang","sex":"man"] print(vdic) vdic.removeValueForKey("age") print("修改后",vdic) //遍历字典 for(key,value) in dic{ print("遍历字典:",key,value) } //元组 var status_let = ("蓝欧",["iOS","123"]) var status:(classroom:String,course:Array) = status_let var classinfo = status.0 + status.1[0] + status.1[1] print(status.1[0]) status_let.1.insert("classroom", atIndex: 1) print(status_let) //分支结构中的switch let sex = "man" switch sex { case "man": print("是man") default: print("0") }
//字符串截取
var str_sub = "123400056789"
//将String转化为NSString再截取
var ns1 = (str_sub as NSString).substringFromIndex(7)
print("from截取后---",ns1) //从index7开始到完,包括index7
var ns2 = (str_sub as NSString).substringToIndex(4)
print("to截取后---",ns2) //从index0开始到4,不包括4
var ns3 = (str_sub as NSString).substringWithRange(NSMakeRange(4,3))
print("范围截取后---",ns3) //从index4开始,截取三位