Objective-C集合

9354 ワード

import
int main(int argc, const char * argv[]) {
//   
//      
//     
//                 ,  nil         
NSArray *array = [[NSArray alloc] initWithObjects:@"a", @"b", @"c", nil];

//           
NSArray *array1 = [NSArray arrayWithObjects:@"1", @"2", @"3", nil];    

//         
//              ,       nil    (       ),         nil   ,        
NSArray *arrya2 = @[@"a", @"b", @"c", @"d"];     

//       
//       
NSLog(@"count: %lu", array.count);    

//              
//            
NSLog(@"object: %@", [array objectAtIndex:2]);

//                  (            ,                ,             ,               )
NSLog(@"index: %lu", [array indexOfObject:@"b"]);

//             
NSLog(@"object: %@", array[0]);

//        
//                       ,       @"."       ,                  
NSString *urlString = @"www.lanou3g.com";
NSArray *resultArray = [urlString componentsSeparatedByString:@"."];
NSLog(@"%@", urlString);
NSLog(@"%@", resultArray);
NSLog(@"lanou result: %@", resultArray[1]);    

//                    
// @"www&lanou3g&com"
NSString *resultString = [resultArray componentsJoinedByString:@"&"];
NSLog(@"result String: %@", resultString);

//            
if ([array containsObject:@"a"]) {
    NSLog(@"  ");
} else {
    NSLog(@"   ");
}
 
/*
         ,             。
 “http://www.imanhua.com/Cover/201110/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg”
*/
NSString *extractString = @"http://www.imanhua.com/Cover/2011-10/hyrz.jpg&http://www.imanhua.com/Cover/2011-09/op.jpg&http://www.imanhua.com/Cover/2012-04/yjdwb.jpg";   
NSArray *extract = [extractString componentsSeparatedByString:@"&"];
NSLog(@"Extraction of string: %@", extract[0]);
    
/*
        ,@[@“type = iOS", @"Device = iPhone", @"count = [email protected]", @"password = 12345”],       ”&"          
 */    
NSArray *joiningString = @[@"type = iOS", @"Device = iPhone", @"count = [email protected]", @"password = 12345"];    
NSString *joining = [joiningString componentsJoinedByString:@" & "];    
NSLog(@"String concatenation: %@", joining);

//              ,               
// nilArray                  ,       
NSArray *emptyArray = [[NSArray alloc] init];
NSArray *nilArray = nil;

//           
NSLog(@"frist: %@", [array firstObject]);
NSLog(@"last: %@", [array lastObject]);

//                 ,              ,        
//     
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];

//                  
NSMutableArray *mutableArray1 = [NSMutableArray array];

//           (     )       
NSMutableArray *mutableArray2 = [[NSMutableArray alloc] initWithObjects:@"a", @"b", @"c", nil];   

//                    
NSMutableArray *mutableArray3 = [NSMutableArray arrayWithArray:array];
NSLog(@"%@", mutableArray3);   

//         
[mutableArray addObject:@"1"];
[mutableArray addObject:@"2"];
[mutableArray addObject:@"w"];
[mutableArray addObject:@"d"];
[mutableArray addObject:@"y"];
[mutableArray addObject:@"w"];
[mutableArray addObject:@"w"];
[mutableArray addObject:@"456"];
[mutableArray addObject:@"w5m"];
[mutableArray addObject:@"2w"];
NSLog(@"add array: %@", mutableArray);

//         
//              (   )
[mutableArray insertObject:@"w" atIndex:0];
NSLog(@"inser array: %@", mutableArray);

//       
[mutableArray replaceObjectAtIndex:1 withObject:@"d"];
NSLog(@"replace array: %@", mutableArray);

//              
mutableArray[2] = @"123";
NSLog(@"result array: %@", mutableArray);

//   
[mutableArray exchangeObjectAtIndex:0 withObjectAtIndex:2];
NSLog(@"exchange array: %@", mutableArray);   

//       
//                ,            
//             ,           
[mutableArray removeObject:@"123"];
NSLog(@"remove array: %@", mutableArray);

//               
[mutableArray removeObjectAtIndex:0];
NSLog(@"remove index array: %@", mutableArray);

//                 
NSUInteger index = [mutableArray indexOfObject:@"w"];
[mutableArray removeObjectAtIndex:index];
NSLog(@"remove frist appear: %@", mutableArray); 

//      ,              
if ([mutableArray containsObject:@"123"]) {        
     NSUInteger index1 = [mutableArray indexOfObject:@"123"];       
     [mutableArray removeObjectAtIndex:index1];
     NSLog(@"remove frist appear: %@", mutableArray);
}

//            
[mutableArray removeObject:@"w" inRange:NSMakeRange(0, 3)];
NSLog(@"remove in range after: %@", mutableArray);

//            
[mutableArray removeObjectsInRange:NSMakeRange(0, 2)];
NSLog(@"remove object in range: %@", mutableArray);

//             
[mutableArray removeObjectsInArray:@[@"w", @"2"]];
NSLog(@"remove in array: %@", mutableArray);

//         
[mutableArray removeLastObject];
NSLog(@"remove last object: %@", mutableArray);

//       
[mutableArray removeAllObjects];
NSLog(@"remove all object: %@", mutableArray);

//                   
[mutableArray addObjectsFromArray:array];
NSLog(@"add object: %@", mutableArray); 

//   
//    key-value key value          
// key      ,       value,    value      key
//        
//      
NSDictionary *dic = [[NSDictionary alloc] initWithObjectsAndKeys:@"99", @"age", @"small tiger", @"name", @"???", @"sxe",  nil];

//      
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"99", @"age", @"small tiger", @"name", @"???", @"sxe", nil];
NSLog(@"dic: %@", dic);

//   key    value
NSLog(@"name: %@", [dic objectForKey:@"name"]);    

//          value
NSLog(@"age: %@", dic[@"age"]);    

//        
NSLog(@"count: %lu", dic.count);

//      value
NSLog(@"all value: %@", dic.allValues);

//      key
NSLog(@"all key: %@", dic.allKeys);



//         
NSDictionary *dic2 = @{@"name": @"small tiger", @"age": @"99", @"sex": @"???"};
NSLog(@"dic2: %@", dic2);    

//   value   key         
NSDictionary *dic3 = [NSDictionary dictionaryWithObjects:@[@"small tiger", @"99", @"???"] forKeys:@[@"name", @"age", @"sex"]];
NSLog(@"dic3: %@", dic3);    

//     
//        
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionary];    

//   (   key   )
[mutableDic setObject:@"add more baby" forKey:@"name"];
[mutableDic setValue:@"10" forKey:@"age"];  // setObject  setValue       , setObject        , setValue         ,             
[mutableDic setObject:@"???" forKey:@"sex"];
NSLog(@"mutableDic: %@", mutableDic);    

//   (     ,    key    )
[mutableDic setObject:@"king old luck" forKey:@"name"];
NSLog(@"mutableDic: %@", mutableDic);    

//         
mutableDic[@"name"] = @"old driver";
NSLog(@"change: %@", mutableDic);    

////あるキー値ペア//[m utableDic removeObjectForKey:@「name」;//NSLog(@"remove: %@", mutableDic);
////キー値ペア//[mutableDic removeAllObjects]//NSLog(@"remove all: %@", mutableDic);
//   key        
[mutableDic removeObjectsForKeys:@[@"age", @"sex"]];
NSLog(@"remove object: %@", mutableDic);    

//   value        key
NSDictionary *testDic = @{@"name": @"small", @"age": @"small", @"sex": @"small"};
NSArray *allKeys = [testDic allKeysForObject:@"small"];
NSLog(@"all key: %@", allKeys);        

//   
//    ,              
//      
NSSet *set = [[NSSet alloc] initWithObjects:@"a", @"b", @"a", @"b", nil];
NSLog(@"set: %@", set);    
NSLog(@"count: %lu", set.count);    

//            ,        
[set anyObject];
NSLog(@"%@", [set anyObject]);

//       
NSArray *setAllObject = [set allObjects];
NSLog(@"all object: %@", setAllObject);

//           
[set containsObject:@"a"];
NSLog(@"%d", [set containsObject:@"a"]);

//     
//       ,    
NSMutableSet *mutableSet = [NSMutableSet setWithArray:@[@"a", @"b", @"a", @"b"]];
NSLog(@"mutable set: %@", mutableSet);

//       (       ,       )
[mutableSet addObject:@"w"];
NSLog(@"add set: %@", mutableSet);    

//       (       ,       )
[mutableSet addObjectsFromArray:@[@"d", @"y", @"a"]];
NSLog(@"add set: %@", mutableSet);

//   
[mutableSet removeObject:@"w"];
NSLog(@"remove set: %@", mutableSet);    

//       
[mutableSet removeAllObjects];
NSLog(@"remove all set: %@", mutableSet);

return 0;

}