美文网首页
oc 基础--NSArray

oc 基础--NSArray

作者: CHUWT | 来源:发表于2016-09-27 23:36 被阅读0次

    // oc 的数组可以存储不同类型的对象
    // oc 的数组只能存储对象 NSNumber NSValue

    // 不可变数组
    NSArray *ffff = @[];
    NSArray *a = [[NSArray alloc] initWithObjects: @"1", @"2", nil];
    // 数组的长度
    int count = (int)a.count;
    // 是否存在
    BOOL ishave = [a containsObject: @"2"];
    // last
    [a lastObject];
    // first
    [a firstObject];
    // 去除所需的元素
    [a objectAtIndex: 1];
    // 如果没有那个值,则为-1
    int index = (int)[a indexOfObject:@"3"];
    
    // 数组的遍历
    // 1. for
    
    for(int i=0;i<(int)a.count;i++){
        NSString *temp = [a objectAtIndex:i];
        NSLog(@"for---%@", temp);
    }
    
    // 2. 快速枚举for in
    // 数组中的元素类型必须保持一致
    for(NSString *str2 in a){
        NSLog(@"for--in--%@", str2);
        NSLog(@"%d", (int)[a indexOfObject:str2]);
    }
    
    // 3.迭代器
    
    // 可变数组
    
    // 初始化
    NSMutableArray *ma = [[NSMutableArray alloc] init];
    // 添加元素  
    [ma addObject: @"11"];
    // 添加数组
    NSArray *array = @[@"1", @"2"];
    [ma addObjectsFromArray: array];
    NSLog(@"%@", ma);
    // 删除元素
    [ma removeObject: @"3"];
    NSLog(@"%@", ma);
    // 删除下标元素
    [ma removeObjectAtIndex: 0];
    // 交换元素位置
    [ma exchangeObjectAtIndex: 1 withObjectAtIndex: 2];

    相关文章

      网友评论

          本文标题:oc 基础--NSArray

          本文链接:https://www.haomeiwen.com/subject/ynrhyttx.html