这里是数组的使用
-
创建一个指定类型以及指定默认值和数组大小的数组,一个是字面量的,一个是NSArray的
var someInts = [Int](repeating: 0, count: 3)//创建一个指定类型以及指定默认值和数组大小的数组 //print(someInts)//0 0 0 someInts[0] = 1; //print(someInts)//1 0 0 someInts.append(2); //print(someInts)//1 0 0 2 //print(someInts+[3])//1 0 0 2 3 someInts.insert(10, at: 0) //print(someInts)//10, 1, 0, 0, 2 someInts.remove(at: 0) //print(someInts)//1, 0, 0, 2 //someInts.removeLast()删除第一个 //someInts.removeLast()//删除最后一个 someInts.removeAll()//删除全部 print(someInts)//
var arr1: NSArray = ["1","2","3","4","5"];//创建一个数据 print(arr1[0]);//1 输出打印某一个值 print(arr1.object(at: 0));//1 和上面是等价的 var arr2: NSArray = NSArray.init(array: arr1) print(arr2);//1 2 3 4 5 var arr3: NSArray = NSArray.init(object: "2"); print("arr3:(arr3)")//2 var arr4: NSArray = NSArray.init(objects: "2","3"); print("arr4:(arr4)")// 2 3
- 可变数组的使用
var arr1: NSArray = ["1","2","3","4","5"]; var arr2: NSMutableArray = NSMutableArray.init(); arr2.addObjects(from: arr1 as! [Any]);//把arr1中所有的obj添加到arr2 print(arr2);//1 2 3 4 5 arr2.add("6")//添加一个对象 print(arr2);//1 2 3 4 5 6 arr2.insert("0", at: 0);//在指定的地方插入一个对象 print(arr2);//0 1 2 3 4 5 6 arr2.remove("0");//删除指定的对象 print(arr2);//1 2 3 4 5 6 arr2.removeLastObject();//删除最后一个对象 print(arr2);//1 2 3 4 5 arr2.removeObject(at: 1);//删除指定索引的对象 print(arr2);//1 3 4 5 arr2.replaceObject(at: 2, with: "10")//覆盖指定索引的对象 print(arr2);//1 3 10 5 arr2.removeAllObjects();//清空数组
- 数组的遍历的两种方式
for obj in arr1 {//数据遍历 print(obj);// 1 2 3 4 5 }
for (index,item) in arr1.enumerated() { print("index:(index) obj:(item)",terminator: " "); //输出结果 index:0 obj:1 index:1 obj:2 index:2 obj:3 index:3 obj:4 index:4 obj:5 }
字典的使用
-
创建一个指定key类型和value类型的字典
//var someDict = [KeyType: ValueType]()//创建一个指定key类型和value类型的字典 var dic1 = [String:String](); dic1["1"] = "one" dic1["2"] = "Two" dic1["3"] = "Three" // print(dic1);//["2": "Two", "1": "one", "3": "Three"] // print(dic1["1"]);//Optional("one") 访问指定key的值 var dic2:[Int:String] = [1:"One", 2:"Two", 3:"Three"] print(dic2);//[3: "Three", 1: "One", 2: "Two"] dic2.updateValue("first", forKey: 1);//修改指定key的值 print(dic2);//[3: "Three", 1: "first", 2: "Two"] dic2.removeValue(forKey: 1);//移除指定key的值 print(dic2);//[3: "Three", 2: "Two"]
- NSDictionary的使用
var dic1: NSDictionary = NSDictionary.init(objects: ["1","2","3"], forKeys: ["one" as NSCopying,"two" as NSCopying,"three" as NSCopying]); print(dic1); { one = 1; three = 3; two = 2; } print(dic1.object(forKey: "one"))//Optional(1) print(dic1.allKeys);//[one, two, three]
-
NSMutableDictionary的使用
var dic1: NSDictionary = NSDictionary.init(objects: ["1","2","3"], forKeys: ["one" as NSCopying,"two" as NSCopying,"three" as NSCopying]); var dic2: NSMutableDictionary = NSMutableDictionary.init(dictionary: dic1); print(dic2); dic2.setObject("4", forKey: "four" as NSCopying);//新增 print(dic2); dic2.removeObject(forKey: "four");//删除 print(dic2) dic2.setObject("11", forKey: "one" as NSCopying)//修改 print(dic2); dic2["one"] = "12"//修改 print(dic2); dic2.removeAllObjects();//删除全部 print(dic2);
- 字典的遍历
for (key, value) in dic2 { print("字典 key (key) - 字典 value (value)") }
for (key, value) in dic2.enumerated() { print("字典 key (key) - 字典 (key, value) 对 (value)") }
- print的基础使用
for x in 0...10{ print("(x) ", terminator: "*")//terminator是指链接符号 } print() print(1.0, 2.0, 3.0, 4.0, 5.0, separator: "*")//separator设置连接符号