Objective-C:Foundation 프레임워크 - 일반 클래스 - NSArray

21899 단어
NSArray는 객체를 저장하는 데 사용되는 일련 번호(NSSet은 순서가 없음)로서 변경될 수 없습니다.NSArray는 int\bloat\enum\struct와 같은 C 언어의 기본 데이터 형식을 저장할 수 없으며 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 }

좋은 웹페이지 즐겨찾기