標籤:
//: Playground - noun: a place where people can playimport UIKitvar str = "Hello, playground"/*數組: 儲存一組有序的資料數組定義:>OC:有值數組NSArray *arr0 = @[@1, @2, @3];NSArray *arr1 = [NSArray arrayWithObjects:@1, @2, @3, nil];NSLog(@"%@", arr1);空數組NSArray *arr2 = @[];NSArray *arr3 = [NSArray array];NSLog(@"%@", arr3);不可變數組:NSArray可變數組:NSMutableArray>Swift:*/// 有值數組var arr0 = [1, 2, 3]var arr1: Array = [1, 2, 3]var arr2: Array<Int> = [1, 2, 3]var arr3: [Int] = [1, 2, 3]//var arr4: Int[] = [1, 2, 3] 早期寫法// 空數組var arr5 = []var arr6 = [Int]()var arr7 = Array<Int>()// 帶初始值的數組var arr8 = Array(count: 5, repeatedValue: 1)println(arr8)// 不可變數組:var arr0 = []// 可變數組:let arr0 = []/*元素類型:元素類型OC:NSArray *arr = @[@1, @"lnj", @1.75];NSLog(@"%@", arr);Swift:*/var arr_02 = [1, "lnj", 1.75]println(arr_02)/*如果想明確表示數組中存放的是不同類型的資料, 可以使用Any關鍵字, 表示數組中可以存放不同類型的資料var arr:Array<Any> = [1, "lnj", 1.75]println(arr)*//*數組操作1.擷取長度OC:NSArray *arr = @[@1, @2, @3];NSLog(@"%tu", arr.count);Swift:var arr = [1, 2, 3]println(arr.count)2.判斷是否為空白OC:NSArray *arr = @[];NSLog(@"%d", arr.count != 0);Swift:*/var arr_01 = [1, 2, 3]println(arr_01.isEmpty)/*3.檢索OC:NSArray *arr = @[@1, @2, @3];NSLog(@"%@", arr[0]);Swift:var arr = [1, 2, 3]println(arr[0])4.追加OC:NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];[arr addObject:@4];NSLog(@"%@", arr);Swift:*/var arr02 = [1, 2, 3]arr02.append(4);println(arr02)var arr03 = [1, 2, 3]arr03 += [4]// arr03 += 4 以前的版本可以這樣寫//arr03 += [5, 6, 7]arr03 += arr03[0...1] // 還可以自己搞自己println(arr03)/*5.插入OC:NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];[arr insertObject:@4 atIndex:0];NSLog(@"%@", arr);Swift:*/var arr = [1, 2, 3]arr.insert(4, atIndex: 0);println(arr)/*6.更新OC:NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];arr[0] = @8;NSLog(@"%@", arr);Swift:*/var arr01 = [1, 2, 3]arr01[0] = 8println(arr01)var arr0002 = [1, 2, 3]arr0002[0..<2] = [8, 9]println(arr0002)/*7.刪除OC:NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];[arr removeObject:@1];NSLog(@"%@", arr);NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];[arr removeLastObject];NSLog(@"%@", arr);NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];[arr removeAllObjects];NSLog(@"%@", arr);Swift:*/var arr001 = [1, 2, 3]arr001.removeAtIndex(0)println(arr001)var arr002 = [1, 2, 3]arr002.removeLast()println(arr002)var arr003 = [1, 2, 3]arr003.removeAll(keepCapacity: false) //是否保持容量, 如果為true, 即便刪除了容量依然存在, 容量是2的倍數println(arr003)println(arr003.capacity)/*注意: 如果數組是一個不可變數組不能更新/插入和刪除第一個版本的不可變數組是可以修改的*//*RangeOC:NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];[arr removeObjectsInRange:NSMakeRange(0, 2)];NSLog(@"%@", arr);Swift:*/var arr_1 = [1, 2, 3]arr_1.removeRange(Range(start: 1, end: 2))println(arr_1)var arr_2 = [1, 2, 3]arr_2.removeRange(0...0)println(arr_2)// 其實Range就是半閉區間var range = 0...5//range = 99 // 通過報錯可以推斷出類型println(range) // 通過列印也可以推斷出類型var range1:Range<Int> = 0...5//var range2:Range<String>; // 必須遵守ForwardIndexType協議// start 起點 end 終點var range3:Range<Int> = Range(start: 0, end: 5)var range4:Range<Int> = 0..<5println(range1)println(range3)println(range4)/*OC:NSMutableArray *arr = [NSMutableArray arrayWithObjects:@1, @2, @3, nil];NSRange range = NSMakeRange(0, 2);// [arr replaceObjectsInRange:range withObjectsFromArray:@[@99, @88]];[arr replaceObjectsInRange:range withObjectsFromArray:@[@99, @88, @77, @66]];NSLog(@"%@", arr);Swift:*///arr.replaceRange(Range(start: 0, end: 2), with: [99, 88, 77, 66])arr.replaceRange(1..<2, with: [99, 88, 77, 66])println(arr)// 等價於上一行代碼var arr_3 = [1, 2, 3]arr_3[Range(start: 0, end: 2)] = [8, 9]println(arr_3)// 等價於上一行代碼var arr_4 = [1, 2, 3]//arr[0...1] = [99, 88]//arr[0...1] = [99, 88, 77, 66]println("====--遍曆--====");/*4.遍曆OC:// for迴圈NSArray *arr = @[@1, @2, @3];for (int i = 0; i < arr.count; i++) {NSLog(@"%@", arr[i]);}// for in迴圈for (NSNumber *number in arr) {NSLog(@"%@", number);}// 迭代器NSArray *arr0 = @[@1, @2, @"1"];[arr0 enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {NSLog(@"index = %tu element = %@", idx, obj);}];Swift:*/// for迴圈var arr_a = [1, 2, 3]for var i = 0 ; i < arr_a.count ; i++{println(arr_a[i])}// for in迴圈for number in arr_a{println(number)}for i in 0..<arr_a.count{println(arr_a[i])}// 取出數組中某個區間範圍的值var arr_b = [1, 2, 3]for number in arr_b[0..<3]{println(number)}// 利用enumerate迭代var arr_c = [1, 2, 3]for (index , value) in enumerate(arr_c){println("index = \(index) value = \(value)")}
|--> Copyright (c) 2015 Bing Ma.
|--> GitHub RUL: https://github.com/SpongeBob-GitHub
Swift # 數組