A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© c9527 中级黑马   /  2015-11-6 08:57  /  878 人查看  /  1 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. //NSArray *array = [NSArray alloc] initWithObjects:
  2.     @"One",@"Two",@"Three",@"Four",nil];

  3.     self.dataArray = array;
  4.     [array release];

  5.     //- (unsigned) Count;数组所包含对象个数;
  6.     NSLog(@"self.dataArray cound:%d",[self.dataArray count]);

  7.     //- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;
  8.     NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);

  9.     //arrayWithArray:
  10.     //NSArray *array1 = [NSArray alloc] init];
  11.     NSMutableArray *MutableArray = [NSMutableArray alloc] init];
  12.     NSArray *array = [NSArray arrayWithObjects:
  13.                       @"a",@"b",@"c",nil];
  14.     NSLog(@"array:%@",array);
  15.     MutableArray = [NSMutableArray arrayWithArray:array];
  16.     NSLog(@"MutableArray:%@",MutableArray);

  17.     array1 = [NSArray arrayWithArray:array];
  18.     NSLog(@"array1:%@",array1);
  19.     //Copy
  20.     //id obj;
  21.     NSMutableArray *newArray = [NSMutableArray alloc] init];
  22.     NSArray *oldArray = [NSArray arrayWithObjects:
  23.                          @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];

  24.     NSLog(@"oldArray:%@",oldArray);
  25.     for(int i = 0; i < [oldArray count]; i++)
  26.     {        
  27.         obj = [oldArray objectAtIndex:i] copy];
  28.         [newArray addObject: obj];
  29.     }
  30.     //     
  31.     NSLog(@"newArray:%@", newArray);
  32.     [newArray release];

  33.     //快速枚举

  34.     //NSMutableArray *newArray = [NSMutableArray alloc] init];
  35.     NSArray *oldArray = [NSArray arrayWithObjects:
  36.                          @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];   
  37.     NSLog(@"oldArray:%@",oldArray);

  38.     for(id obj in oldArray)
  39.     {
  40.         [newArray addObject: obj];
  41.     }
  42.     //     
  43.     NSLog(@"newArray:%@", newArray);
  44.     [newArray release];   

  45.     //Deep copy
  46.     //NSMutableArray *newArray = [NSMutableArray alloc] init];
  47.     NSArray *oldArray = [NSArray arrayWithObjects:
  48.                          @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil];   
  49.     NSLog(@"oldArray:%@",oldArray);   
  50.     newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers);
  51.     NSLog(@"newArray:%@", newArray);
  52.     [newArray release];   

  53.     //Copy and sort

  54.     //NSMutableArray *newArray = [NSMutableArray alloc] init];
  55.     NSArray *oldArray = [NSArray arrayWithObjects:
  56.                          @"b",@"a",@"e",@"d",@"c",@"f",@"h",@"g",nil];   
  57.     NSLog(@"oldArray:%@",oldArray);
  58.     NSEnumerator *enumerator;
  59.     enumerator = [oldArray objectEnumerator];
  60.     id obj;
  61.     while(obj = [enumerator nextObject])
  62.     {
  63.         [newArray addObject: obj];
  64.     }
  65.     [newArray sortUsingSelector:@selector(compare:)];
  66.     NSLog(@"newArray:%@", newArray);
  67.     [newArray release];

  68.     //从字符串分割到数组- componentsSeparatedByString:
  69.     NSString *string = [NSString alloc] initWithString:@"One,Two,Three,Four"];
  70.     NSLog(@"string:%@",string);   
  71.     NSArray *array = [string componentsSeparatedByString:@","];
  72.     NSLog(@"array:%@",array);
  73.     [string release];

  74.     //从数组合并元素到字符串- componentsJoinedByString:
  75.     NSArray *array = [NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
  76.     NSString *string = [array componentsJoinedByString:@","];
  77.     NSLog(@"string:%@",string);
  78.    
  79.     //NSArray *array;
  80.     array = [NSMutableArray arrayWithCapacity:20];   
  81.     //- (void) addObject: (id) anObject;
  82.     //NSMutableArray *array = [NSMutableArray arrayWithObjects:
  83.     @"One",@"Two",@"Three",nil];
  84.     [array addObject:@"Four"];
  85.     NSLog(@"array:%@",array);

  86.     //-(void) removeObjectAtIndex: (unsigned) index;   
  87.     //NSMutableArray *array = [NSMutableArray arrayWithObjects:
  88.     @"One",@"Two",@"Three",nil];
  89.     [array removeObjectAtIndex:1];
  90.     NSLog(@"array:%@",array);        
  91.     //- (NSEnumerator *)objectEnumerator;从前向后
  92.     //NSMutableArray *array = [NSMutableArray arrayWithObjects:
  93.     @"One",@"Two",@"Three",nil];
  94.     NSEnumerator *enumerator;
  95.     enumerator = [array objectEnumerator];

  96.     id thingie;
  97.     while (thingie = [enumerator nextObject]) {
  98.         NSLog(@"thingie:%@",thingie);
  99.     }

  100.     //- (NSEnumerator *)reverseObjectEnumerator;从后向前
  101.     //NSMutableArray *array = [NSMutableArray arrayWithObjects:
  102.     @"One",@"Two",@"Three",nil];
  103.     NSEnumerator *enumerator;
  104.     enumerator = [array reverseObjectEnumerator];

  105.     id object;
  106.     while (object = [enumerator nextObject]) {
  107.         NSLog(@"object:%@",object);
  108.     }

  109.     //快速枚举
  110.     //NSMutableArray *array = [NSMutableArray arrayWithObjects:
  111.     @"One",@"Two",@"Three",nil];
  112.     for(NSString *string in array)
  113.     {
  114.         NSLog(@"string:%@",string);
  115.     }
  116.     //- (id) initWithObjectsAndKeys;

  117.     //NSDictionary *dictionary = [NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
  118.     NSString *string = [dictionary objectForKey:@"One"];
  119.     NSLog(@"string:%@",string);
  120.     NSLog(@"dictionary:%@",dictionary);
  121.     [dictionary release];   
  122.     //创建
  123.     NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];

  124.     //添加字典
  125.     [dictionary setObject:@"One" forKey:@"1"];
  126.     [dictionary setObject:@"Two" forKey:@"2"];
  127.     [dictionary setObject:@"Three" forKey:@"3"];
  128.     [dictionary setObject:@"Four" forKey:@"4"];
  129.     NSLog(@"dictionary:%@",dictionary);

  130.     //删除指定的字典
  131.     [dictionary removeObjectForKey:@"3"];
  132.     NSLog(@"dictionary:%@",dictionary);

  133.     //将NSRect放入NSArray中
  134.     NSMutableArray *array = [NSMutableArray alloc] init];
  135.     NSValue *value;
  136.     CGRect rect = CGRectMake(0, 0, 320, 480);   
  137.     value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)];
  138.     [array addObject:value];
  139.     NSLog(@"array:%@",array);

  140.     //从Array中提取
  141.     value = [array objectAtIndex:0];
  142.     [value getValue:&rect];
  143.     NSLog(@"value:%@",value);

  144.     //NSFileManager *fileManager = [NSFileManager defaultManager];
  145.     NSString *home;
  146.     home = @"../Users/";

  147.     NSDirectoryEnumerator *direnum;
  148.     direnum = [fileManager enumeratorAtPath: home];

  149.     NSMutableArray *files = [NSMutableArray alloc] init];

  150.     //枚举
  151.     NSString *filename;
  152.     while (filename = [direnum nextObject]) {
  153.         if([filename pathExtension] hasSuffix:@"jpg"]){
  154.             [files addObject:filename];
  155.         }
  156.     }

  157.     //快速枚举
  158.     //for(NSString *filename in direnum)
  159.     //{
  160.     //    if([filename pathExtension] isEqualToString:@"jpg"]){
  161.     //        [files addObject:filename];
  162.     //    }
  163.     //}
  164.     NSLog(@"files:%@",files);

  165.     //枚举
  166.     NSEnumerator *filenum;
  167.     filenum = [files objectEnumerator];
  168.     while (filename = [filenum nextObject]) {
  169.         NSLog(@"filename:%@",filename);
  170.     }

  171.     //快速枚举
  172.     //for(id object in files)
  173.     //{
  174.     //    NSLog(@"object:%@",object);
  175.     //}
复制代码

1 个回复

倒序浏览
受教了,很好的笔记
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马