NSMutableArray常见用法

337人阅读 评论(0) 收藏 举报
分类:
#import <Foundation/Foundation.h>

@interface Person : NSObject
@end
@implementation Person
@end


int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        /* 数组可以包括任意类型 */
        Person *p = [[Person alloc] init];
        NSMutableArray *arr1 = [[NSMutableArray alloc] initWithObjects: @"One String", @"Two", @"Three", p, nil];
        
        /* 数组中的每个对象实际是该对象的地址 */
        /* NSLOG打印时,调用的是对象的description方法,字符串返回本身,对象为多个C类型字符串则返回时包含双引号 */
        NSLog(@"arr1-%@", arr1);
        
        /* 利用已知数组初始化新数组 */
        NSMutableArray *arr2 = [[NSMutableArray alloc] initWithArray: arr1];
        /* 利用已知数组初始化时的地址浅拷贝 */
        NSMutableArray *arr3 = [[NSMutableArray alloc] initWithArray: arr1 copyItems: NO];
        
        /* 生成空的数组后追加 */
        NSMutableArray *arr4 = [[NSMutableArray alloc] init];
        [arr4 addObject:@"First String"];
        [arr4 addObject:@"Second"];
        
        /* 使用枚举器遍历数组 */
        NSEnumerator *enumarator = [arr2 objectEnumerator];
        id anObject;
        while (anObject = [enumarator nextObject]) {
            NSLog(@"arr2 object-%@", anObject);
        }
        
        /* 快速遍历法 */
        for (anObject in arr3)
        {
            NSLog(@"arr3 object--%@", anObject);
        }
        
        /* i遍历法 */
        NSInteger i = 0;
        for ( ; i < [arr4 count]; i++)
        {
            NSLog(@"arr4 object---%@", [arr4 objectAtIndex:i]);
        }
        
        /* 删除部分或全部元素 */
        [arr2 removeObject:@"Two"];
        NSLog(@"arr2 is %@", arr2);
        [arr2 removeAllObjects];
        NSLog(@"arr2 is %@", arr2);
        [arr4 removeObject:@"First String"];
        NSLog(@"arr4 is %@", arr4);
        
        /* 元素交换 */
        [arr3 exchangeObjectAtIndex:0 withObjectAtIndex: 2];
        NSLog(@"arr3 is %@", arr3);
        
        /* 注意M.a.c输出时以字符串形式 */
        NSString *str1 = @"This is my M.a.c book";
        NSArray *arr5 = [str1 componentsSeparatedByString: @" "];
        NSLog(@"arr5 is %@", arr5);
        
        /* 逆序 */
        NSMutableArray *arr6 = [[NSMutableArray alloc] init];
        NSEnumerator *enumra = [arr5 reverseObjectEnumerator];
        while (anObject = [enumra nextObject]) {
            [arr6 addObject: anObject];
        }
        NSLog(@"arr6 is %@", arr6);
        
        /* 拼接成字符串 */
        NSMutableString *str2 = [[NSMutableString alloc] init];
        id obj;
        for (obj in arr6)
        {
            [str2 appendFormat:@"%@ ", obj];
        }
        NSLog(@"str2 is %@", str2);
    }
    return 0;
}

输出结果:

2015-11-18 22:39:53.637 TestMutableArray[646:18692] arr1-(
    "One String",
    Two,
    Three,
    "<Person: 0x100206a00>"
)
2015-11-18 22:39:53.641 TestMutableArray[646:18692] arr2 object-One String
2015-11-18 22:39:53.641 TestMutableArray[646:18692] arr2 object-Two
2015-11-18 22:39:53.641 TestMutableArray[646:18692] arr2 object-Three
2015-11-18 22:39:53.641 TestMutableArray[646:18692] arr2 object-<Person: 0x100206a00>
2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr3 object--One String
2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr3 object--Two
2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr3 object--Three
2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr3 object--<Person: 0x100206a00>
2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr4 object---First String
2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr4 object---Second
2015-11-18 22:39:53.642 TestMutableArray[646:18692] arr2 is (
    "One String",
    Three,
    "<Person: 0x100206a00>"
)
2015-11-18 22:39:53.643 TestMutableArray[646:18692] arr2 is (
)
2015-11-18 22:39:53.643 TestMutableArray[646:18692] arr4 is (
    Second
)
2015-11-18 22:39:53.643 TestMutableArray[646:18692] arr3 is (
    Three,
    Two,
    "One String",
    "<Person: 0x100206a00>"
)
2015-11-18 22:39:53.643 TestMutableArray[646:18692] arr5 is (
    This,
    is,
    my,
    "M.a.c",
    book
)
2015-11-18 22:39:53.643 TestMutableArray[646:18692] arr6 is (
    book,
    "M.a.c",
    my,
    is,
    This
)
2015-11-18 22:39:53.644 TestMutableArray[646:18692] str2 is book M.a.c my is This 





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:402080次
    • 积分:4627
    • 等级:
    • 排名:第7039名
    • 原创:87篇
    • 转载:2篇
    • 译文:0篇
    • 评论:380条
    博客专栏
    文章分类
    最新评论