NSArray使用小结

#import 


int main(int argc, const char * argv[])
{

    @autoreleasepool {
      
        //类方法数组创建
        NSArray *array1 = [NSArray arrayWithObject:@"obj"];
        NSArray *array2 = [NSArray arrayWithObjects:@"obj1", @"obj2", @"obj3", nil];
        NSArray *array3 = [NSArray arrayWithArray:array2];
        NSLog(@"array1 :%@", array1);
        NSLog(@"array2 :%@", array2);
        NSLog(@"array3 :%@", array3);
        
        //实例方法创建数组
        NSArray *array4 = [[NSArray alloc] initWithObjects:@"AAA", @"bbb", nil];
        NSLog(@"array4 :%@", array4);
        
        //数组个数
        NSLog(@"array3 count :%d", [array3 count]);
        
        //访问元素
        NSLog(@"obj at index :%@", [array2 objectAtIndex:2]);
        
        //追加(返回新的array对象)
        NSArray *array5 = [array3 arrayByAddingObject:@"ccc"];
        NSLog(@"array5 :%@", array5);
        
        
        //根据指定的字符串连接数组元素
        NSString *joinString1 = [array5 componentsJoinedByString:@"|"];
        NSLog(@"joinString :%@",joinString1);
        
        
        //是否包含指定对象
        NSLog(@"isContains :%d", [array5 containsObject:@"obj2"]);
        
        
        //查找某个对象所在索引
        NSLog(@"indexOfObject :%d",[array5 indexOfObject:@"obj3"]);
        
        //最后一个元素
        NSLog(@"lastObejct :%@", [array5 lastObject]);
        
        //遍历数组
        for (id element in array5) {
            NSLog(@"element :%@", element);
        }
        
        
        
        
        //可变数组(期望容量设置为3)
        NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:3];
        //直接添加
        [mutableArray addObject:@"aaa"];
        [mutableArray addObject:@"eee"];
        [mutableArray addObject:@"000"];
        NSLog(@"addObject :%@", mutableArray);
        
        //插入元素
        [mutableArray insertObject:@"ccc" atIndex:0];
        NSLog(@"insertObject :%@", mutableArray);
        
        //移除指定元素
        [mutableArray removeObject:@"ccc"];
        NSLog(@"removeObject :%@", mutableArray);
        
        //移除指定下标元素
        [mutableArray removeObjectAtIndex:0];
        NSLog(@"removeObjectAtIndex :%@", mutableArray);
        
        //移除最后一个元素
        [mutableArray removeLastObject];
        NSLog(@"removeLastObject :%@", mutableArray);
        
        //添加数组
        [mutableArray addObjectsFromArray:array5];
        NSLog(@"addObjectsFromArray :%@", mutableArray);
        
        
        //移除指定数组中的内容
        [mutableArray removeObjectsInArray:array2];
        NSLog(@"removeObjectsInArray :%@", mutableArray);
        
        
        //指定索引替换对象
        [mutableArray replaceObjectAtIndex:0 withObject:@"==="];
        NSLog(@"replaceObjectAtIndex :%@", mutableArray);
        
        
        
        
        //删除全部对象
        [mutableArray removeAllObjects];
        NSLog(@"removeAllObjects :%@", mutableArray);
    }
    return 0;
}


日志:

2013-03-10 01:52:47.710 FoundationDemo[6215:303] array1 :(
    obj
)
2013-03-10 01:52:47.712 FoundationDemo[6215:303] array2 :(
    obj1,
    obj2,
    obj3
)
2013-03-10 01:52:47.713 FoundationDemo[6215:303] array3 :(
    obj1,
    obj2,
    obj3
)
2013-03-10 01:52:47.714 FoundationDemo[6215:303] array4 :(
    AAA,
    bbb
)
2013-03-10 01:52:47.714 FoundationDemo[6215:303] array3 count :3
2013-03-10 01:52:47.715 FoundationDemo[6215:303] obj at index :obj3
2013-03-10 01:52:47.715 FoundationDemo[6215:303] array5 :(
    obj1,
    obj2,
    obj3,
    ccc
)
 joinString :obj1|obj2|obj3|ccc
 isContains :1
 indexOfObject :2
 lastObejct :ccc
 element :obj1
 element :obj2
 element :obj3
 element :ccc
 addObject :(
    aaa,
    eee,
    000
)
 insertObject :(
    ccc,
    aaa,
    eee,
    000
)
 removeObject :(
    aaa,
    eee,
    000
)
 removeObjectAtIndex :(
    eee,
    000
)
 removeLastObject :(
    eee
)
 addObjectsFromArray :(
    eee,
    obj1,
    obj2,
    obj3,
    ccc
)
 removeObjectsInArray :(
    eee,
    ccc
)
 replaceObjectAtIndex :(
    "===",
    ccc
)
 removeAllObjects :(
)




你可能感兴趣的:(IOS)