• swift基础语法(08-数组操作)


    获取长度
    OC:
    NSArray *arr = @[@1, @2, @3];
    NSLog(@"%tu", arr.count);
    输出结果: 2016-04-01 01:19:54.874 test[12086:2209180] 3
     
    swift:
    var arr = [1, 2, 3]
    print(arr.count)
    输出结果:3
     
    判断是否为空
    OC:
    NSArray *arr = @[];
    NSLog(@"result = %d", arr.count != 0);
    输出结果: 2016-04-01 01:25:14.252 OCTest[12249:2255010] result = 0
            
    swift:
    var arr = [1, 2, 3]
    print(arr.isEmpty)
    输出结果: false
     
    检索
    OC:
    NSArray *arr = @[@1, @2, @3];
    NSLog(@"%@", arr[0]);
    输出结果: 2016-04-01 01:26:46.816 OCTest[12275:2269853] 1
    swift:
    var arr = [1, 2, 3]
    print(arr[0])
    输出结果: 1
     
    追加
    OC:
    NSMutableArray *arr =
    [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr addObject:@4];
    NSLog(@"%@", arr);
    输出结果:
    2016-04-01 01:28:47.111 OCTest[12297:2286198] (
        1,
        2,
        3,
        4
    )
    swift:
    var arr = [1, 2, 3]
    arr.append(4);
    print(arr)
    输出结果: [1, 2, 3, 4]
     
    var arr = [1, 2, 3]
    arr += [4]
    print(arr)
    输出结果: [1, 2, 3, 4]
     
    var arr = [1, 2, 3]
    arr += [4]
    arr += [5,6,7]
    输出结果: [1, 2, 3, 4, 5, 6, 7]
     
    var arr = [1, 2, 3]
    arr += [4]
    arr += [5,6,7]
    arr += arr[0...3]
    print(arr)
    输出结果: [1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4]
     
    插入
    OC:
    NSMutableArray *arr =
    [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr insertObject:@4 atIndex:0];
    NSLog(@"%@", arr);
    输出结果:
    2016-04-01 01:36:10.450 OCTest[12374:2339079] (
        4,
        1,
        2,
        3
    )
    swift:
    var arr = [1, 2, 3]
    arr.insert(4, atIndex: 0);
    print(arr)
    输出结果: [4, 1, 2, 3]
     
    更新
    OC:
    NSMutableArray *arr =
    [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    arr[0] = @8;
    NSLog(@"%@", arr);
    输出结果:
    2016-04-01 01:37:28.482 OCTest[12397:2350177] (
        8,
        2,
        3
    )
    swift:
    var arr = [1, 2, 3]
    arr[0] = 8
    print(arr)
    输出结果: [8, 2, 3]
     
    删除
    OC:
    NSMutableArray *arr1 =
    [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr1 removeObject:@1];
    NSLog(@"%@", arr1);
    NSMutableArray *arr2 =
    [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr2 removeLastObject];
    NSLog(@"%@", arr2);
    NSMutableArray *arr3 =
    [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr3 removeAllObjects];
    NSLog(@"%@", arr3);
    输出结果:
    2016-04-01 01:39:49.831 OCTest[12418:2370779] (
        2,
        3
    )
    2016-04-01 01:39:49.832 OCTest[12418:2370779] (
        1,
        2
    )
    2016-04-01 01:39:49.832 OCTest[12418:2370779] (
    )
    swift:
    var arr1 = [1, 2, 3]
    arr1.removeAtIndex(0)
    print(arr1)

    var arr2 = [1, 2, 3]
    arr2.removeLast()
    print(arr2)
    var arr3 = [1, 2, 3]
    是否保持容量, 如果为true, 即便删除了容量依然存在, 容量是2的倍数
    arr3.removeAll(keepCapacity: false)
    print(arr3)
    print(arr3.capacity)
    输出结果:
    [2, 3]
    [1, 2]
    []
    0
     
    Range
    OC:
    NSMutableArray *arr =
    [NSMutableArray arrayWithObjects:@1, @2, @3, nil];
    [arr removeObjectsInRange:NSMakeRange(0, 2)];
    NSLog(@"%@", arr);
    输出结果:
    2016-04-01 01:43:17.863 OCTest[12441:2396986] (
        3
    )
    swift:
    var arr = [1, 2, 3]
    arr.removeRange(Range(start: 1, end: 2))
    print(arr)
    输出结果: [1, 3]
    var arr = [1, 2, 3]
    arr.removeRange(0...0)
    print(arr)
    输出结果: [2, 3]
     
    var range = 0...5
    print(range)
    输出结果: 0..<6
     
    我们每一种习惯都是由一再重复的行为所铸造的,因此,优秀不是一种行为,而是一种习惯.
  • 相关阅读:
    禁用网络连接后无法访问本机数据库的问题
    DevExpress使用笔记
    DEV控件 皮肤问题
    SQLServer2008导入Excel遇到的问题
    InnoSetup使用笔记
    SQLServer清空数据库中所有表的数据
    MS SQL Server2000转换成MySQL
    由MySQL登录不了引发的一些问题
    onerror事件
    DIV+CSS一些小小的技巧
  • 原文地址:https://www.cnblogs.com/jordanYang/p/5378190.html
Copyright © 2020-2023  润新知