Foundation NSArray

来源:互联网 发布:泰拉瑞亚哥布林数据 编辑:程序博客网 时间:2024/06/06 08:37



////  main.m//  NSArray////  Created by LiuWei on 15/4/18.//  Copyright (c) 2015年 LiuWei. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h"// 数组的创建void arrayCreate();// 数组的使用void usageOfArray();// 数组的内存管理void memoryManagement();// 数组的遍历void ergodic();// 其它void others();int main(){    // arrayCreate();    // usageOfArray();    // memoryManagement();    // ergodic();    others();    return 0;}#pragma mark - 数组的创建void arrayCreate(){    //NSArray只能存放非空的OC对象        // 创建并返回一个空数组    NSArray *arr = [NSArray array];    NSLog(@"%@", arr);        // 创建拥有一个元素的数组    arr = [NSArray arrayWithObject:@"good"];    NSLog(@"%@", arr);    arr = @[@222]; // 以字面量表达式的方式创建拥有一个元素的数组    NSLog(@"%@", arr);        // 创建拥有多个元素的数组 nil表示元素的结束    arr = [NSArray arrayWithObjects:@"good", @22, @33.4,  nil];    NSLog(@"%@", arr);    // 以字面量表达式的方式创建拥有多个元素的数组    arr = @[@"1233", @233, @444, @"time"];    NSLog(@"%@", arr);        // 返回数组元素个数    NSLog(@"arr 有 %ld 个元素", arr.count);    }#pragma mark - 数组的使用void usageOfArray(){    NSArray *arr = @[@123, @"hello", @9384, @"well"];        // 判断数组中是否包含指定元素, 包含返回YES, 否则返回NO    if ([arr containsObject:@"hello"])    {        NSLog(@"包含");    }    else    {        NSLog(@"不包含");    }        // 返回数组中最后一个元素    id lastObj = [arr lastObject];    NSLog(@"%@", lastObj);        // 获取指定索引处元素    NSLog(@"%@", [arr objectAtIndex:2]);        // 获取指定元素所在的索引 如果指定元素在数组中不存在, 则返回 NSNotFound    NSLog(@"%ld", [arr indexOfObject:@"hello"]);        // 在指定索引范围内查找元素, 并返回元素索引, 没找到则返回NSNotFound    NSRange range = NSMakeRange(1, 2);    NSLog(@"%ld", [arr indexOfObject:@123 inRange:range]);    }#pragma mark - 数组的内存管理void memoryManagement(){    Student *stu1 = [[Student alloc]init];    stu1.name = @"小华";    Student *stu2 = [[Student alloc]init];    stu2.name = @"小明";    Student *stu3 = [[Student alloc]init];    stu3.name = @" 小伟";        NSLog(@"%ld, %ld, %ld", [stu1 retainCount], [stu2 retainCount], [stu3 retainCount]);        // 对象放到数组里边, 对象的计数器会加1    NSArray *arr = [[NSArray alloc]initWithObjects:stu1, stu2, stu3, nil];        NSLog(@"%ld, %ld, %ld", [stu1 retainCount], [stu2 retainCount], [stu3 retainCount]);        // 给数组中所有元素发送一条test 消息    // SEL selector = NSSelectorFromString(@"test");    [arr makeObjectsPerformSelector:@selector(test)];        [arr makeObjectsPerformSelector:@selector(test:) withObject:@"hello"];        [stu1 release];    [stu2 release];    [stu3 release];    // 数组销毁时会对所有元素发送一条release消息    [arr release];    }#pragma mark - 数组的遍历void ergodic(){    Student *stu1 = [[Student alloc]init];    stu1.name = @"小华";    Student *stu2 = [[Student alloc]init];    stu2.name = @"小明";    Student *stu3 = [[Student alloc]init];    stu3.name = @" 小伟";        NSArray *arr = @[stu1, stu2, stu3];        // 一般的遍历方法    NSUInteger count = arr.count;    for (int i = 0; i < count; i++)    {        [arr[i] test:@"hi"];                // [[arr objectAtIndex:i] test:@"hi";    }            // 快速遍历    for (id obj in arr)    {        [obj test:@"haha!"];    }        // 遍历方法3    [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)    {        [obj test:@"xixi"];        NSLog(@"idx = %ld", idx);        if (idx == 1)        {            *stop = YES; // 终止遍历        }    }];        // 遍历方法4    // 返回一个 enumerator对象以访问数组中从低索引到高索引的每个对象    NSEnumerator * enumer = [arr objectEnumerator];    // enumer = [arr reverseObjectEnumerator]; // 反序迭代        id obj;    while (obj = [enumer nextObject]) // nextObject 获取集合中下一个对象, 如果不存在,则返回nil    {        [obj test:@"he he!"];    }    enumer = [arr objectEnumerator];    NSArray *arr2 = [enumer allObjects]; // 返回迭代器中的所有元素    NSLog(@"%@", arr2);        [stu1 release];    [stu2 release];    [stu3 release];    [arr release];    [arr2 release];}#pragma mark - 其它void others(){    @autoreleasepool    {        Student *stu1 = [Student student];        stu1.name = @"小华";        Student *stu2 = [Student student];        stu2.name = @"小明";        Student *stu3 = [Student student];        stu3.name = @" 小伟";                NSArray *arr = @[stu1, stu2];        NSLog(@"%p, %@", arr, arr);                // 在新生成的数组中添加一个指定元素        NSArray *arr2 = [arr arrayByAddingObject:stu3];        NSLog(@"%p, %@", arr2, arr2);                        // 把当前数组中所有元素和参数给定数组中所有元素按顺序生成一个新的数组        NSArray *arr3 = [arr2 arrayByAddingObjectsFromArray:arr];        id obj;        for (obj in arr3)        {            [obj test:@"111"];        }                // 截取数组 根据指定范围截取并生成一个新的数组        NSRange range = NSMakeRange(2, 3);        NSArray *arr4 = [arr3 subarrayWithRange:range];        for (obj in arr4)        {            [obj test];        }                // 以指定的字符串连接数组中的元素        NSString *str = [arr4 componentsJoinedByString:@" - "];        NSLog(@"%@", str);                // 数组写入到文件        NSArray *newArray = @[@"have", @"a", @"good", @"time", @1223];        NSString *path = @"/Users/liuwei/Desktop/testString.txt";        [newArray writeToFile:path atomically:YES];                // 从文件中读取到数组 文件有严格的格式要求        NSArray *fromFile = [NSArray arrayWithContentsOfFile:path];        NSLog(@"%@", fromFile);            }        }


0 0
原创粉丝点击