字符串 String
1.是一个Unicode 编码,16位字符的字符序列
2.与 NSString 支持无缝互操作
3.被定义为 struct 拷贝时具有值语义
4.虽然是一个struct 但内部包含一个指向堆上的内容指针(特殊),指向对象存放真正内容
5.变量字符串 var str;常量字符串let str
6.获取字符串中的字符:
var str1 = "Hello"
//枚举字符
for item in str1.characters{
print(item)
}
for index in str1.characters.indices{
print(str1[index])
}
7.使用 append 和+连接字符串
//--连接字符串--
str1.appendContentsOf(str2)//m1
var str3 = str1 + str2//m2
var char:Character = "!"
str1.append(char)
8.字符转义
//--字符串转义--
let heart = "\u{2665}"
let words = "\tHello"
9.字符串插值
//--字符串插值--
var x=100,y=200.0
var text = "[\(x),\(y)]"
10.字符串比较
//--字符串比较--
var text1 = "Swift",text2 = "swift"
print(text1 == text2)//大小写敏感
//忽略大小写
if(text1.rangeOfString(text2,
options: NSStringCompareOptions.CaseInsensitiveSearch) == nil){
print("no find")
}
else{
print("find")
}
copy on write
1.同一字符串拷贝到不同变量,不同变量共享同一份内存,支持字符串随时改变
2.尽量不要改变字符串,用静态的 let
集合类型
1.array
一个有序的元素序列,支持随机存取,动态长度
2.set
- 无序集合,其存储的值不能重复
- 集合元素必须有哈希值(支持 Hashable 协议)
3.dictionary
- 无序集合,存储 key-value, 其中 key 唯一, value 课重复
- key 必须有哈希值
注意:都为,struct 类型,值类型,但是同 string 一样,内部包含一个指向堆上的元素指针
array
import Foundation
//--数组的声明与实例化--
var array1 = [1,2,3,4,5]
var array2:[Int]
var array3:Array<Int>
array2 = [Int](count:5,repeatedValue:10)
array3 = [Int]()
//--内存模型--
print(sizeofValue(array1))
print(strideofValue(array1))
print(sizeofValue(array2))
print(strideofValue(array2))
print(sizeofValue(array3))
print(strideofValue(array3))
array3.append(1)
array3.append(2)
array3.append(3)
print(sizeofValue(array3))
print(strideofValue(array3))
class MyClass{
init(){
print("MyClass.init")
}
deinit{
print("MyClass.deinit")
}
}
func process(){
var objectArray=[MyClass]()
for _ in 1...5{
objectArray.append(MyClass())
}
print(objectArray.count)
objectArray.removeAll()
}
process()
//--变量数组和常量数组--
var array5 = [1,2,3]
let array6 = [1,2,3]
array5.append(40)
array5[2]=4
print(array5)
//--数组操作--
for item in array5{
print(item)
}
print("排序:")
for item in array5.sort( > ){
print("\(item)")
}
for(index, value) in array5.enumerate(){
print("\(index): \(value)")
}
for index in 0..<array5.count {
print(array5[index])
}
var array7=array5+array6
array5+=[5,6,7]
print(array5.count)
array5[4...6]=[6,7,8]
print(array5)
array5.insert(5, atIndex:4) //代价较大
print(array5)
array5.removeAtIndex(7) //代价较大
print(array5)
//--缓存容量--
var array8=[1,2,3]
print("\(array8.count), \(array8.capacity)")
array8.reserveCapacity(120)//分配大小
for index in 4...100{
array8.append(index)
print("\(array8.count), \(array8.capacity)")
}
//--copy-on-write--
var array4=array1
array1[0]=10
array1.append(20)
print(array1)
print(array4)
//--排序--
var arr = [4,3,5,2,6,1]
arr.sortInPlace()
arr.sortInPlace {$0 > $1} // *** [1, 2, 3, 4, 5, 6]
arr = [4,3,5,2,6,1]
arr.sortInPlace(>) // *** [1, 2, 3, 4, 5, 6]
print(arr)
set
var words = Set<String>()
words.insert("Hello")
words.insert("Swift")
words.insert("Language")
words.insert("Swift")
print(sizeofValue(words))
words.count
words=[]
words.count
print(sizeofValue(words))
var cities1:Set<String>
cities1=["Shanghai","Beijing","New York","Shanghai"]
cities1.count
for city in cities1{
print(city)
}
for city in cities1.sort(){//排序
print(city)
}
for city in cities1.sort( > ){//反序
print(city)
}
cities1.count
cities1.contains("New York")
var cities2:Set=["New York", "Shanghai"]
print(cities1.isSupersetOf(cities2))
print(cities1==cities2)
cities1.remove("Beijing")
print(cities1==cities2)
print(cities1.isSupersetOf(cities2))
dictionary
import Foundation
var dictionary1 = [String:Int]()
var dictionary2 : Dictionary<String,Int>
dictionary2=["Jason":36, "Tom":31, "Marty":44]
print(sizeofValue(dictionary1))
print(sizeofValue(dictionary2))
for(name, age) in dictionary2{
print("The Age of \(name) is: \(age)")
}
for name in dictionary2.keys{
print(name)
}
for name in dictionary2.keys.sort(){
print(name)
}
for value in dictionary2.values{
print(value)
}
for value in dictionary2.values.sort({$0 > $1}){
print(value)
}
dictionary2.count
dictionary2["Tom"]=32
dictionary2.updateValue(33,forKey:"Tom")
dictionary2["Julie"]=28
dictionary2.count
dictionary2["Marty"]=nil
dictionary2.removeValueForKey("Tom")
dictionary2.count