• Objective-C:Foundation框架-常用类-NSArray


      NSArray是用来存储对象的有序列表(NSSet是没有顺序的),它是不可变的。NSArray不能存储C语言中的基本数据类型,如intfloatenumstruct等,也不能存储nil。其用法如下:

      1 #pragma mark 创建一个数组
      2 void arrayCreate() {
      3     // 创建一个空的数组
      4     NSArray *array = [NSArray array];
      5     
      6     // 创建有1个元素的数组
      7     array = [NSArray arrayWithObject:@"123"];
      8     
      9     // 创建有多个元素的数组
     10     array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
     11     
     12     int count = [array count];
     13     // count = array.count;
     14     NSLog(@"%i", count);
     15 }
     16 
     17 #pragma mark 数组的简单使用
     18 void arrayUse() {
     19     NSObject *obj = [[NSObject alloc] init];
     20     NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c" , obj, nil];
     21     // 判断是否包含了某个元素
     22     if ([array containsObject:@"a"]) {
     23         NSLog(@"包含了字符串a");
     24     }
     25     
     26     NSString *last = [array lastObject];
     27     NSLog(@"last=%@", last);
     28     
     29     NSString *str = [array objectAtIndex:1];
     30     NSLog(@"%@", str);
     31     
     32     int index = [array indexOfObject:@"c"];
     33     NSLog(@"index=%i", index);
     34     
     35     [obj release];
     36 }
     37 
     38 #pragma mark 数组的内存管理
     39 void arrayMemory() {
     40     // 1
     41     Student *stu1 = [[Student alloc] init];
     42     Student *stu2 = [[Student alloc] init];
     43     Student *stu3 = [[Student alloc] init];
     44     
     45     NSLog(@"stu1:%zi", [stu1 retainCount]);
     46     
     47     // 当把一个对象塞进数组中时,这个对象的计数器会加1,也就是说数组会对它做一次retain操作
     48     // 2
     49     NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil];
     50     
     51     NSLog(@"stu1:%zi", [stu1 retainCount]);
     52     
     53     NSLog(@"count=%zi", array.count);
     54     
     55     // 1
     56     [stu1 release];
     57     [stu2 release];
     58     [stu3 release];
     59     
     60     // 数组被销毁的时候,会对内部的所有元素都做一次release操作
     61     // 0
     62     [array release];
     63 }
     64 
     65 #pragma mark 给数组里面的元素发送消息
     66 void arrayMessage() {
     67     Student *stu1 = [Student student];
     68     Student *stu2 = [Student student];
     69     Student *stu3 = [Student student];
     70     
     71     NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil];
     72     // 让数组里面的所有对象都调用test方法
     73     // [array makeObjectsPerformSelector:@selector(test)];
     74     [array makeObjectsPerformSelector:@selector(test2:) withObject:@"123"];
     75 }
     76 
     77 #pragma mark 遍历数组1
     78 void arrayFor1() {
     79     Student *stu1 = [Student student];
     80     NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
     81     int count = array.count;
     82     for (int i = 0; i<count; i++) {
     83         // id == void *
     84         id obj = [array objectAtIndex:i];
     85         NSLog(@"%i-%@", i, obj);
     86     }
     87 }
     88 
     89 #pragma mark 遍历数组2
     90 void arrayFor2() {
     91     Student *stu1 = [Student student];
     92     NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
     93     // 快速遍历
     94     int i =0;
     95     for (id obj in array) {
     96         NSLog(@"%i-%@", i, obj);
     97         i++;
     98     }
     99 }
    100 
    101 #pragma mark 遍历数组3
    102 void arrayFor3() {
    103     Student *stu1 = [Student student];
    104     NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
    105     [array enumerateObjectsUsingBlock:
    106      ^(id obj, NSUInteger idx, BOOL *stop) {
    107         NSLog(@"%i-%@", idx, obj);
    108          
    109          // 如果索引为1,就停止遍历
    110          if (idx == 1) {
    111              // 利用指针修改外面BOOL变量的值
    112              *stop = YES;
    113          }
    114     }];
    115 }
    116 
    117 #pragma mark 遍历数组4
    118 void arrayFor4() {
    119     Student *stu1 = [Student student];
    120     NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
    121     
    122     // 获取数组的迭代器
    123     // NSEnumerator *enumerator = [array objectEnumerator];
    124     // 反序迭代器(从尾部开始遍历元素)
    125     NSEnumerator *enumerator = [array reverseObjectEnumerator];
    126     
    127     // allObjects是取出没有被遍历过的对象
    128     NSArray *array2 = [enumerator allObjects];
    129     NSLog(@"array2:%@", array2);
    130     
    131     // 获取下一个需要遍历的元素
    132     id obj = nil;
    133     while (obj = [enumerator nextObject]) {
    134         NSLog(@"obj=%@", obj);
    135     }
    136 }
    #import <Foundation/Foundation.h>
    
    @interface Book : NSObject
    @property (nonatomic, retain) NSString *name;
    
    + (id)bookWithName:(NSString *)name;
    @end
    
    #import "Book.h"
    
    @implementation Book
    
    + (id)bookWithName:(NSString *)name {
        Book *book = [[[Book alloc] init] autorelease];
        book.name = name;
        return book;
    }
    
    - (void)dealloc {
        [_name release];
        [super dealloc];
    }
    @end
      1 #pragma mark 派生出新的数组
      2 void arrayNew() {
      3     NSArray *array = [NSArray arrayWithObjects:@"1", @"2", nil];
      4     
      5     NSArray *array2 = [array arrayByAddingObject:@"3"];
      6     
      7     NSArray *array3 = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"4", @"5", nil]];
      8     
      9     NSLog(@"array:%@", array);
     10     NSLog(@"array2:%@", array2);
     11     NSLog(@"array3:%@", array3);
     12     
     13     
     14     NSArray *array4 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
     15     NSRange range = NSMakeRange(1, 2);
     16     NSArray *array5 = [array4 subarrayWithRange:range];
     17     NSLog(@"array5:%@", array5);
     18 }
     19 
     20 #pragma mark 数组的其他用法
     21 void arrayOther() {
     22     NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
     23     // 1-2-3-4
     24     // 利用分隔符-拼接所有的数组元素
     25     NSString *str = [array componentsJoinedByString:@"-"];
     26     NSLog(@"%@", str);
     27     
     28     // 将一个数组写入文件(生成的是一个xml文件)
     29     NSString *path = @"/Users/apple/Desktop/array.xml";
     30     [array writeToFile:path atomically:YES];
     31     
     32     
     33     path = @"/Users/apple/Desktop/array.txt";
     34     // 从文件中读取数组内容(文件有严格的格式要求)
     35     NSArray *array2 = [NSArray arrayWithContentsOfFile:path];
     36     NSLog(@"array2:%@", array2);
     37 }
     38 
     39 #pragma mark 数组排序1
     40 void arraySort1() {
     41     NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil];
     42     
     43     // 返回一个排好序的数组,原来数组的元素顺序不会改变
     44     // 指定元素的比较方法:compare:
     45     NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
     46     NSLog(@"array2:%@", array2);
     47 }
     48 
     49 #pragma mark 数组排序2
     50 void arraySort2() {
     51     Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];
     52     Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];
     53     Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];
     54     Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];
     55     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
     56     
     57     // 指定排序的比较方法
     58     NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];
     59     
     60     NSLog(@"array2:%@", array2);
     61 }
     62 
     63 #pragma mark 数组排序3
     64 void arraySort3() {
     65     Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];
     66     Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];
     67     Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];
     68     Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];
     69     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
     70     
     71     // 利用block进行排序
     72     NSArray *array2 = [array sortedArrayUsingComparator:
     73      ^NSComparisonResult(Student *obj1, Student *obj2) {
     74          // 先按照姓排序
     75          NSComparisonResult result = [obj1.lastname compare:obj2.lastname];
     76          // 如果有相同的姓,就比较名字
     77          if (result == NSOrderedSame) {
     78              result = [obj1.firstname compare:obj2.firstname];
     79          }
     80          
     81          return result;
     82     }];
     83     
     84     NSLog(@"array2:%@", array2);
     85 }
     86 
     87 #pragma mark 数组排序4-高级排序
     88 void arraySort4() {
     89     Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"];
     90     Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang" bookName:@"book2"];
     91     Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"];
     92     Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao" bookName:@"book1"];
     93     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
     94     
     95     // 1.先按照书名进行排序
     96     // 这里的key写的是@property的名称
     97     NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
     98     // 2.再按照姓进行排序
     99     NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
    100     // 3.再按照名进行排序
    101     NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
    102     // 按顺序添加排序描述器
    103     NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil];
    104     
    105     NSArray *array2 = [array sortedArrayUsingDescriptors:descs];
    106     
    107     NSLog(@"array2:%@", array2);
    108 }
  • 相关阅读:
    使用servletContext和类加载器加载文件
    servletConfig和servletContext的应用
    java中的正则表达式(一)
    servlet的生命周期
    servlet的基本原理
    java中类加载机制
    java中的动态代理(三)
    Navicat Premium 连接Oracle 数据库
    使用SqlServer2005 Service Broker 和 SqlDependency类提供数据更改的通知
    WebService简单使用
  • 原文地址:https://www.cnblogs.com/yif1991/p/5068136.html
Copyright © 2020-2023  润新知