OC-NSArray

7330 단어
2.OC    
   OC  ?
 :a.OC               ( :  );     C         ;  ,          。
------------------------------------------------------------------------------------
1)  :          NSArray             NSMutableArray
------------------------------------------------------------------------------------
2)    OC        
   :
NSArray * array = [[NSArrayalloc]initWithObjects:@"one",@"two",@"three",nil];
//nil OC             ,      

   :
//       (array)   ,           (array1)
NSArray * array1 = [NSArrayarrayWithArray:array];
------------------------------------------------------------------------------------
3)           

   :
//             ,      
  :
NSString * str = [arrayobjectAtIndex:0];

   :
//        
  :
NSUInteger count = array.count;
------------------------------------------------------------------------------------
4)     (      )

  ://          ,       ,   

   :
//            
NSEnumerator * enumer1  = [arrayobjectEnumerator];        
//    ://          ,               ;                  

   :
//         
  :
id obj;
while (obj = [enumer1nextObject]) 
{
   NSLog(@"obj = %@",obj);
}
    :
     ,               obj;     ,              obj,        , nil  obj.

   :
//      (  )    
  :
for (id obj2in array) 
{
   NSLog(@"obj2 = %@",obj2);
}
    :
     ,               obj2;     ,              obj2,        , nil  obj2.

   :
//     
  :
//              
NSEnumerator * enumer2 = [arrayreverseObjectEnumerator];
//                 obj            obj         ;
while (obj = [enumer2nextObject]) 
{
   NSLog(@"obj = %@",obj);
}
------------------------------------------------------------------------------------
5)      

   :
//      ,         (        )       NSNotFound
NSUInteger index = [arrayindexOfObject:@"one"];

   :
//        ;
NSUIntegerindex = [arrayindexOfObject:@"one"inRange:NSMakeRange(1, array.count -1)];

***********************************************************************
3.   /     

1)        
Class cls = [dog1class];
//  dog1          
------------------------------------------------------------------------------------
2)                
[Dogclass];//  Dog    
------------------------------------------------------------------------------------
3)//  id       ,            
id obj;
BOOL ret = [objisKindOfClass:[Dogclass]];
//  obj        Dog        

***********************************************************************
4.                description    
    :         ,               description ,             description  ,      ,       (NSObject   description         ),                   ,       description  。

  :
NSString * str =@"welcome to beijing";
NSLog(@"%@",str);//[str description]
Dog *dog = [[Dogalloc]initWithAge:10name:@"Tom"];
NSLog(@"dog = %@",dog);//                 [dog description];
NSLog(@"dog = %@",[dogdescription]);//      ;      description  ;
//        dog           (age name  )       description  


   description
  :
//           (            )
- (NSString *)description
{
   NSString * str = [NSStringstringWithFormat:@"age = %d name = %@",age,name];
   return str;
}


***********************************************************************
5.      
   :
NSString * str =@"You are a good  man!";
NSArray * array  = [strcomponentsSeparatedByString:@" "];//            
NSLog(@"str = %@",str);
NSLog(@"array = %@",array);
***  //           NSArray*;

   :
array = [strcomponentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@" !"]];//        
    :
//NSCharacterSet      
//[NSCharacterSet characterSetWithCharactersInString:@" !"];        
// Oc   @“ !”       ;
//          ' '   ‘!’;
  :        
//OC         ,                             ,            @"";
//@"good !man!";man   !                @"";
//  good !;   !          (           )                
***********************************************************************
6.     
  :
NSString * ptr = [arraycomponentsJoinedByString:@"###"];
//       @”###“             ;
***  //            :NSString*
***********************************************************************
7.      

1)        
   :
NSArray * array1 = [[NSArrayalloc]initWithObjects:@"1",@"2",nil];

   :
//             
NSMutableArray * array2 = [NSMutableArrayarrayWithArray:array1];

   :
//          
NSMutableArray * array3 = [[NSMutableArrayalloc]init ];


2)        : 、 、 、  

--------- ----------
//      
[array4addObject:@"iOS"];

--------- ----------
   :
//  "ios"    ,      ,    
[array4removeObject:@"iOS"];

   :
//    
[array4removeAllObjects];

   :
//        
[array4removeLastObject];

   :
//           
[array4removeObject:@"iOS"inRange:NSMakeRange(1,3)];

--------- ----------
[array4replaceObjectAtIndex:0withObject:@"Android"];//  (    )

---------  ----------
[array4exchangeObjectAtIndex:0withObjectAtIndex:4];//  


NSSet:
           ,                     

배열 정렬
여러 가지 방법으로 하나의 수조를 정렬한다.배열이 문자열 객체인 경우 sortedArrayUsingSelector: 가 첫 번째 선택입니다.

  
  
  
  
  1. NSArray *array = @[@"John Appleseed", @"Tim Cook", @"Hair Force One", @"Michael Jurewitz"]; 
  2. NSArray *sortedArray = [array sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]; 

 
다음 코드는 NSNumber가 compare를 구현했기 때문에 디지털 저장에도 유용합니다.

  
  
  
  
  1. NSArray *numbers = @[@9, @5, @11, @3, @1]; 
  2. NSArray *sortedNumbers = [numbers sortedArrayUsingSelector:@selector(compare:)]; 

좋은 웹페이지 즐겨찾기