萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> ios >> ios中NSArray,NSSet,NSDictionary總結

ios中NSArray,NSSet,NSDictionary總結

  三種集合類來收集cocoa對象(NSObject對象):

  NSArray 用於對象有序集合(相當於是數組)

  NSSet 用於對象無序集合

  NSDictionary用於鍵值映射

  以上三種集合類是不可變的(一旦初始化後,就不能改變)

  以下是對應的三種可變集合類(這三種可變集合類是對應上面三種集合類的子類):

  NSMutableArray

  NSMutableSet

  NSMutableDictionary

  注:這些集合類只能收集cocoa對象(NSOjbect對象),如果想保存一些原始的C數據(例如,int, float, double, BOOL等),則需要將這些原始的C數據封裝成NSNumber類型的,NSNumber對象是cocoa對象,可以被保存在集合類中。

  NSArray

  Ordered collection of objects. Immutable. You cannot add or remove objects to it once it’s created.

  Important methods:

  + (id)arrayWithObjects:(id)firstObject, ...; // nil terminated

  - (int)count;

  - (id)objectAtIndex:(int)index; // NSString *s1=[[myarray objectAtIndex: 0];

  - (void)makeObjectsPerformSelector:(SEL)aSelector;

  - (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;

  - (id)lastObject; // returns nil if there are no objects in the array (convenient)

  注:

  類方法arrayWithObjects 可以創建an autoreleased NSArray of the items.例如

  @implementation MyObject

  - (NSArray *)coolCats {

  return [NSArray arrayWithObjects:@“Steve”, @“Ankush”, @“Sean”, nil];

  }

  @end

  Other convenient create with methods (all return autoreleased objects):

  [NSString stringWithFormat:@“Meaning of %@ is %d”, @“life”, 42];

  [NSDictionary dictionaryWithObjectsAndKeys:ankush, @“TA”, janestudent, @“Student”, nil];

  [NSArray arrayWithContentsOfFile:(NSString *)path];

  -----創建數組 -----

  //NSArray *array = [[NSArray alloc] initWithObjects:

  @"One",@"Two",@"Three",@"Four",nil];

  self.dataArray = array;

  [array release];

  //- (unsigned) Count;數組所包含對象個數;

  NSLog(@"self.dataArray cound:%d",[self.dataArray count]);

  //- (id) objectAtIndex: (unsigned int) index;獲取指定索引處的對象;

  NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);

  ------ 從一個數組拷貝數據到另一數組(可變數級) -------

  //arrayWithArray:

  //NSArray *array1 = [[NSArray alloc] init];

  NSMutableArray *MutableArray = [[NSMutableArray alloc] init];

  NSArray *array = [NSArray arrayWithObjects:

  @"a",@"b",@"c",nil];

  NSLog(@"array:%@",array);

  MutableArray = [NSMutableArray arrayWithArray:array];

  NSLog(@"MutableArray:%@",MutableArray);

  array1 = [NSArray arrayWithArray:array];

  NSLog(@"array1:%@",array1);

  //Copy

  //id obj;

  NSMutableArray *newArray = [[NSMutableArray alloc] init];

  NSArray *oldArray = [NSArray arrayWithObjects:

  @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];

  NSLog(@"oldArray:%@",oldArray);

  for(int i = 0; i < [oldArray count]; i++)

  {

  obj = [[oldArray objectAtIndex:i] copy];

  [newArray addObject: obj];

  }

  //

  NSLog(@"newArray:%@", newArray);

  [newArray release];

  //快速枚舉

  //NSMutableArray *newArray = [[NSMutableArray alloc] init];

  NSArray *oldArray = [NSArray arrayWithObjects:

  @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];

  NSLog(@"oldArray:%@",oldArray);

  for(id obj in oldArray)

  {

  [newArray addObject: obj];

  }

  //

  NSLog(@"newArray:%@", newArray);

  [newArray release];

  //Deep copy

  //NSMutableArray *newArray = [[NSMutableArray alloc] init];

  NSArray *oldArray = [NSArray arrayWithObjects:

  @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];

  NSLog(@"oldArray:%@",oldArray);

  newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);

  NSLog(@"newArray:%@", newArray);

  [newArray release];

  //Copy and sort

  //NSMutableArray *newArray = [[NSMutableArray alloc] init];

  NSArray *oldArray = [NSArray arrayWithObjects:

  NSMutableArray

  Mutable version of NSArray.

  - (void)addObject:(id)anObject;

  - (void)insertObject:(id)anObject atIndex:(int)index;

  - (void)removeObjectAtIndex:(int)index;

  - (void)removeAllObjects;

  -----給數組分配容量-----

  //NSArray *array;

  array = [NSMutableArray arrayWithCapacity:20];

  -----在數組末尾添加對象-----

  //- (void) addObject: (id) anObject;

  //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];

  [array addObject:@"Four"];

  NSLog(@"array:%@",array);

  -----刪除數組中指定索引處對象-----

  //-(void) removeObjectAtIndex: (unsigned) index;

  //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];

  [array removeObjectAtIndex:1];

  NSLog(@"array:%@",array);

  ----- 數組枚舉-----

  //1、- (NSEnumerator *)objectEnumerator; //從前向後

  //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil];

  NSEnumerator *enumerator;

  enumerator = [array objectEnumerator];

  id thingie;

  while (thingie = [enumerator nextObject]) {

  NSLog(@"thingie:%@",thingie);

  }

  //2、- (NSEnumerator *)reverseObjectEnumerator; //從後向前

  //NSMutableArray *array = [NSMutableArray arrayWithObjects:

  @"One",@"Two",@"Three",nil];

  NSEnumerator *enumerator;

  enumerator = [array reverseObjectEnumerator];

  id object;

  while (object = [enumerator nextObject]) {

  NSLog(@"object:%@",object);

  }

  //3、快速枚舉

  //NSMutableArray *array = [NSMutableArray arrayWithObjects:

  @"One",@"Two",@"Three",nil];

  for(NSString *string in array)

  {

  NSLog(@"string:%@",string);

  }

  ----- NSValue(對任何對象進行包裝)-----

  //將NSRect放入NSArray中

  NSMutableArray *array = [[NSMutableArray alloc] init];

  NSValue *value;

  CGRect rect = CGRectMake(0, 0, 320, 480);

  value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];

  [array addObject:value];

  NSLog(@"array:%@",array);

  //從Array中 提取

  value = [array objectAtIndex:0];

  [value getValue:&rect];

  NSLog(@"value:%@",value);

  ----★使用NSMutableArray要防止內存洩露★------

  NSObject* p1 = [[NSObject alloc] init];

  NSObject* p2 = [[NSObject alloc] init];

  NSMu

copyright © 萬盛學電腦網 all rights reserved