当前位置:编程学习 > wap >>

Iphone数组一些基础操作NSArray/NSMutableArray

  1 /*******************************************************************************************
  2      NSArray
  3      *******************************************************************************************/
  4
  5     /*---------------------------创建数组------------------------------*/
  6     //NSArray *array = [NSArray alloc] initWithObjects:
  7     @"One",@"Two",@"Three",@"Four",nil];
  8
  9     self.dataArray = array;
 10     [array release];
 11
 12     //- (unsigned) Count;数组所包含对象个数;
 13     NSLog(@"self.dataArray cound:%d",[self.dataArray count]);
 14
 15     //- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;
 16     NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);
 17
 18
 19     /*--------------------------从一个数组拷贝数据到另一数组(可变数级)----------------------------*/   
 20
 21     //arrayWithArray:
 22 //NSArray *array1 = [NSArray alloc] init];
 23     NSMutableArray *MutableArray = [NSMutableArray alloc] init];
 24     NSArray *array = [NSArray arrayWithObjects:
 25                       @"a",@"b",@"c",nil];
 26     NSLog(@"array:%@",array);
 27     MutableArray = [NSMutableArray arrayWithArray:array];
 28     NSLog(@"MutableArray:%@",MutableArray);
 29
 30     array1 = [NSArray arrayWithArray:array];
 31     NSLog(@"array1:%@",array1);
 32
 33
 34     //Copy
 35
 36 //id obj;
 37     NSMutableArray *newArray = [NSMutableArray alloc] init];
 38     NSArray *oldArray = [NSArray arrayWithObjects:
 39                          @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];
 40
 41     NSLog(@"oldArray:%@",oldArray);
 42     for(int i = 0; i < [oldArray count]; i++)
 43     {       
 44         obj = [oldArray objectAtIndex:i] copy];
 45         [newArray addObject: obj];
 46     }
 47     //
 48     NSLog(@"newArray:%@", newArray);
 49     [newArray release];
 50
 51
 52     //快速枚举
 53
 54 //NSMutableArray *newArray = [NSMutableArray alloc] init];
 55     NSArray *oldArray = [NSArray arrayWithObjects:
 56                          @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];   
 57     NSLog(@"oldArray:%@",oldArray);
 58
 59     for(id obj in oldArray)
 60     {
 61         [newArray addObject: obj];
 62     }
 63     //
 64     NSLog(@"newArray:%@", newArray);
 65     [newArray release];   
 66
 67
 68     //Deep copy
 69
 70 //NSMutableArray *newArray = [NSMutableArray alloc] init];
 71     NSArray *oldArray = [NSArray arrayWithObjects:
 72                          @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];   
 73     NSLog(@"oldArray:%@",oldArray);   
 74     newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
 75     NSLog(@"newArray:%@", newArray);
 76     [newArray release];   
 77
 78
 79     //Copy and sort
 80
 81 //NSMutableArray *newArray = [NSMutableArray alloc] init];
 82     NSArray *oldArray = [NSArray arrayWithObjects:
 83                          @"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];   
 84     NSLog(@"oldArray:%@",oldArray);
 85     NSEnumerator *enumerator;
 86     enumerator = [oldArray objectEnumerator];
 87     id obj;
 88     while(obj = [enumerator nextObject])
 89     {
 90         [newArray addObject: obj];
 91     }
 92     [newArray sortUsingSelector:@selector(compare:)];
 93     NSLog(@"newArray:%@", newArray);
 94     [newArray release];
 95
 96
 97
 98     /*---------------------------切分数组------------------------------*/
 99
100     //从字符串分割到数组- componentsSeparatedByString:
101     NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];
102     NSLog(@"string:%@",string);   
103     NSArray *array = [string componentsSeparatedByString:@","];
104     NSLog(@"array:%@",array);
105     [string release];
106
107
108     //从数组合并元素到字符串- componentsJoinedByString:
109     NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
110&nbs

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,