1.有拷贝(copy)行为的写法
var list:Array = ["a","b","c",4,5]
var a = list
var b = a
a.insert(1, atIndex: 0)
println(list)
println(a)
println(b)
//打印日志
[a, b, c, 4, 5]
[1, a, b, c, 4, 5]
[a, b, c, 4, 5]
2.字符拼接字符串
var str :String = ""
for (var i = 0;i < 32 ; i++){
var index = arc4random_uniform(26) + 65 //随机产生数字
var a :Character = Character (UnicodeScalar(index)) //创建字符
var subStr = String (a)//字符转化成string
str += subStr //拼接
}
println(str)
3.字符串中的字符
for item in "iloveyou" {
println(item)
}
4.字符串中某一个字符
var str = "abcvalinvla"
var char:Character = Character (str.substringWithRange(Range (start: advance(str.startIndex, 3), end: advance(str.startIndex, 4))))
println(char)
5.字典
Swift中数组字典其实都是特殊的集合,数组是元组为一个的特殊集合,字典是元组为2个的特殊集合。
var dic = [NSObject : NSObject]()
dic.updateValue(1, forKey: "key1")//更新键值对
dic.updateValue(2, forKey: "key2")
dic.updateValue(3, forKey: "key3")
for (item1,item2) in dic {//所以遍历的时候用元组形式
println ( item2 )
}
//字典中的key或者value 也可以说 自定义的type 或者枚举
enum type {
case one
case two
case three
case four
case five
}
var dic = [type : NSObject]()
dic.updateValue(1, forKey: type.one)
dic.updateValue(2, forKey: type.three)
dic.updateValue(3, forKey: type.five)
for key in dic.keys {
println(key) //打印所有的key
}
for value in dic.values {
println(value) //打印所有的values
}