iOS_nsarray_nsdictionary知识点总结

来源:互联网 发布:php大于等于 编辑:程序博客网 时间:2024/05/21 01:54
cc
    //**************************************************                 //   三 . NSArray    //***********************************************   //用NSNumber 封装int 类型的i变量为对象类型    double i = 100.99 ;    NSNumber *num_1 = [NSNumber numberWithDouble:i];    NSLog(@"%@",num_1);    NSInteger j = 10;    NSNumber *num_3 = [NSNumber numberWithInteger:j];    NSLog(@"%@",num_3);    NSArray *arry_4 = [[NSArray alloc] initWithObjects:num_1,@"d",@"ios", nil];    for (int i = 0 ; i < [arry_4 count]; i++) {        NSLog(@"%@",[arry_4 objectAtIndex:i]);    }    //intvalue 将NSNumber的对象类型,转换成int类型    int a_1 = [[arry_4 objectAtIndex:0] intValue];    NSLog(@"%i",a_1 );//创建int型对象    NSNumber *num = [[NSNumber alloc] initWithInt:12];    printf("\n");    //isKindOfClass判断是不是一个类的实例    BOOL b=[arry_4 isKindOfClass:[NSString class]];    NSLog(@"%i",b);    NSInteger integer = [arry_4 indexOfObject:@"ios"];    NSLog(@"%ld",integer);     // @[] 只创建不可变数组NSArray  ->  NSArray *array = @[@"jack", @"rose"];    //Person *persons[5] = {p,  [[Person alloc] init]};    //数组元素访问array3[1]和array[@"chen"];                //----------数组元素:可以是任意对象类型的指针-----------//数组创建    NSString *s1 = [[NSString alloc] initWithFormat:@"haoren %@",@"chen"];          //此处对象会加上双引号    NSArray *array1 = [[NSArray alloc] initWithObjects:@"liang",@"shi",s1, nil];    NSArray *array2 = [[NSArray alloc] initWithArray:array1];        NSArray *array3 =[[NSArray alloc] initWithObjects:@"chen",@"liang", nil];    NSArray *array4 = [ NSArray arrayWithArray:array3];    //数组遍历    //NSLog(@"%@,%@",array2,array4);    for (int i = 0 ; i < [array4 count]; i++) {        NSLog(@"%@",[array4 objectAtIndex:i]);    }        for(id arry1 in array2){//快速枚举的语法 for(id 变量名 in 数组名)        NSLog(@"%@",arry1);    }           NSArray *array5 = [[NSArray alloc] initWithObjects:@"ios",@"haoren",num, nil];    for (int i = 0 ; i < [array5 count]; i++) {        NSLog(@"%@",[array5 objectAtIndex:i]);    }//找对应元素的下标    NSLog(@"%lu",[array5 indexOfObject:@"haoren"]);    //判断数组中是否包含这个元素    NSLog(@"%i",[array5 containsObject:@"chen_liang"]);//获取数组中最后一个元素    NSLog(@"%@",[array5 lastObject]);    //数组中字符串加上字符后拼接(没有改变array5)    NSLog(@"%@",[array5 componentsJoinedByString:@"陈梁"]);    NSLog(@"%@",array5);     //创建一个NSMutableArray的对象    //实例方法创建对象    //initWithObjects    NSMutableArray *mutableArray = [[NSMutableArray alloc] initWithObjects:@"ios",@"haoren", nil];    NSLog(@"%@",mutableArray);        //initWithArray    NSMutableArray *mutableArray_1 = [[NSMutableArray alloc] initWithArray:array5];    NSLog(@"%@",mutableArray_1);    //类方法创建对象    //arrayWithObjects    NSMutableArray *mutableArray_2 = [NSMutableArray arrayWithObjects:@"ios",@"haoren" ,Nil];    NSLog(@"%@",mutableArray_2);    //arrayWithArray    NSMutableArray *mutableArray_3 =[NSMutableArray arrayWithArray:mutableArray_1];    NSLog(@"%@",mutableArray_3);//1增加    //数组总增加元素 ->addObject    NSNumber *num_2 = [[NSNumber alloc] initWithInt:1234] ;    [mutableArray_3 addObject:num_2 ];    NSLog(@"%@",mutableArray_3);    //指定元素插入 (数组元素不能越界)    [mutableArray_3 insertObject:@"haoren" atIndex:1];    NSLog(@"%@",mutableArray_3);    //添加整个数组    [mutableArray_3 addObjectsFromArray:mutableArray_1];    NSLog(@"%@",mutableArray_3);//2删除//删除最后一个元素    [mutableArray_3 removeLastObject];    NSLog(@"%@",mutableArray_3);    //删除指定索引元素    [mutableArray_3 removeObjectAtIndex:1];    NSLog(@"%@",mutableArray_3);//删除所有元素    //[mutableArray_2 removeAllObjects];    NSLog(@"%@",mutableArray_2);   //删除指定元素    [mutableArray_3 removeObject:@"ios"];    NSLog(@"%@",mutableArray_3);    //在一定范围内删除元素---->(数组 一定 不能越界)    [mutableArray_3 removeObject:@"1234" inRange:NSMakeRange(0, 4) ];    NSLog(@"%@",mutableArray_3);//3修改    //用其他数组取代    [mutableArray_3 setArray:mutableArray_2];    NSLog(@"%@",mutableArray_3);//4替换指定元素    [mutableArray_3 replaceObjectAtIndex:0 withObject:@"haoren"];    NSLog(@"%@",mutableArray_3);    //5交换数组元素    [mutableArray_3 exchangeObjectAtIndex:0 withObjectAtIndex:1];    NSLog(@"%@",mutableArray_3);      //**************************************************      //              二.NSDictionary     // **********************************************          //创建一个字典    NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"ios",@"1", @"ios",@"2",@"student",@"3", nil];    //取出所有的值    NSArray * array = [dictionary allKeysForObject:@"student"];    NSLog(@"%@",array);    //取出对应键值的值    NSString *obj = [dictionary objectForKey:@"name"];    NSLog(@"%@",obj);    //快速枚举遍历    for (NSString *my_key in dictionary) {        NSLog(@"key:%@\t\tvalue:%@",my_key,[dictionary objectForKey:my_key]);    }    //枚举遍历    NSEnumerator *enu = [dictionary objectEnumerator] ;    id obj_1;    while (obj_1 = [enu nextObject]) {        NSLog(@"%@",obj_1);    }    NSLog(@"%@",obj_1);        NSLog(@"%lu",[dictionary count]);    //取出所有的键值    NSArray *array_1 = [dictionary allValues];    for (NSString *value in array_1) {        NSLog(@"%@",value);    }        NSArray *array_2 = [dictionary allKeys];    for (int i = 0 ; i < [dictionary count]; i++) {        NSLog(@"%@",[array_2 objectAtIndex:i]);    }//找出值对应的所有键值    NSArray *array_3 = [dictionary allKeysForObject:@"ios"];    NSLog(@"%@",array_3);       //NSMutableDictionary *dict1 = @{@"name": @"chenliagn"};-->不能创建可变字典//NSMutableDictionary    NSMutableDictionary *mutabledictionary = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"ios",@"4",@"haoren",@"5",@"student",@"6", nil];//当键值重复,最后面的会代替前面的    //NSLog(@"%@",mutabledictionary);    //添加一个键值,当字典中存在是会覆盖    [mutabledictionary setObject:@"ios" forKey:@"7"];    for (NSString *key_ in mutabledictionary) {        NSLog(@"%@",[mutabledictionary objectForKey: key_]);    }    //删除对象    [mutabledictionary removeObjectForKey:@"name"];    NSLog(@"%@",mutabledictionary);    //用数组实现删除    NSArray *array_4 = [NSArray arrayWithObjects:@"2",@"1", nil];    [mutabledictionary removeObjectsForKeys:array_4];    NSLog(@"%@",mutabledictionary);        //[mutabledictionary removeAllObjects ];    NSLog(@"%@",mutabledictionary);    //根据已有字典初始化    NSDictionary *dictionary_1 = [[NSDictionary alloc]initWithObjectsAndKeys:@"ios",@"2",@"student",@"4", nil];    //NSMutableDictionary *dictionary_2 = [NSMutableDictionary setDictionary:dictionary_1];    //增加一个字典    [mutabledictionary addEntriesFromDictionary:dictionary_1];    NSLog(@"%@",mutabledictionary);        NSLog(@"%lu",[mutabledictionary count]);        NSArray *array_5 = [mutabledictionary allValues];    NSLog(@"%@",array_5);        NSArray *array_6 = [mutabledictionary allKeys];    NSLog(@"%@",array_6);// 1、NSMutableDictionary 和NSDictionary 遍历访问是无序的,for(id obj in zidian)        // 2、 NSMutableDictionary 和NSDictionary 用NSLog打印对象,是有序的,顺序是按照你的key 从小到大排序        for (NSString *str in mutabledictionary) {        NSLog(@"%@",str);    }//    for (int i = 0 ; i < [mutabledictionary count]; i++) {//        NSLog(@"%@",[mutabledictionary objectForKey:@"i"]);//    }    //删除对应key值的键值对    [mutabledictionary removeObjectForKey:@"5"];    NSLog(@"%@",mutabledictionary);    NSArray *array_7 = [[NSArray alloc]initWithObjects:@"2",@"7", nil];    [mutabledictionary removeObjectsForKeys:array_7];    NSLog(@"%@",mutabledictionary);    //修改    //用其他字典改变整个字典    [mutabledictionary setDictionary:dictionary];    NSLog(@"%@",mutabledictionary);    //修改键值对        

0 0
原创粉丝点击