其他分享
首页 > 其他分享> > OC数组和字典简单使用

OC数组和字典简单使用

作者:互联网

//1)创建一个空数组
        NSArray *arr1 = [NSArray array];

        //2)创建数组,只有一个元素
        NSArray *arr2 = [NSArray arrayWithObject:@"1"];

        //4)调用对象方法,创建数组
        //nil Nil NULL  NSNULL
        NSArray *arr4 = [[NSArray alloc] initWithObjects:@"three",[NSNull null],@"four", nil];
                        NSLog(@"arr4 = %@",arr4);
        
        NSArray *arr3 = [NSArray arrayWithObjects:@"one",@"two",@1,@"three", nil];
        NSLog(@"arr3 = %@",arr3);

        //1)获取数组的长度  count获取数组的元素的个数
        NSLog(@"%ld", [arr3 count]);

        //2)根据下标,获取下标对应的对象
        NSLog(@"%@",[arr3 objectAtIndex:3]);

        //3)返回元素的下标(首个)
        NSUInteger loc = [arr3 indexOfObject:@"three"];
        NSLog(@"%ld",loc);

        //4)数组中是否包含了某个元素
        if([arr3 containsObject:@"four"]){
            
            NSLog(@"包含此元素");
            
        }else{
            
            NSLog(@"不包含");
        }
        
        NSMutableArray *mutableArray = [NSMutableArray array];
        [mutableArray addObject:@"fuck"];
        [mutableArray addObjectsFromArray: arr3];
        [mutableArray removeObject: @"three"];
        for (NSString *str in mutableArray) {
            NSLog(@"%@", str);
        }
//        //枚举器遍历数组
//        NSEnumerator *myEnumerator = [arr3 objectEnumerator];
//        id temp;
//        while (temp = myEnumerator.nextObject) {
//            NSLog(@"%@", temp);
//        }
//        //普通for循环遍历数组
//        for (int i = 0; i < [arr3 count]; ++i) {
//            NSLog(@"%@", arr3[i]);
//        }
//        //for循环快速枚举遍历数组
//        for (NSString *str in arr3) {
//            NSLog(@"%@", str);
//        }
        //创建一个空的字典

        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithDictionary:dict];
        NSMutableDictionary *dict3 = [NSMutableDictionary dictionaryWithObject: @"wangziyou" forKey:@"wzy"];
        [dict addEntriesFromDictionary:dict3];
        [dict removeObjectForKey:@"wzy"];
        [dict setObject:@"fuckyou" forKey:@"fy"];
        [dict setObject:@"wutingting" forKey:@"wzy"];
        NSLog(@"%@", dict);

 

标签:OC,NSLog,NSMutableDictionary,arr3,数组,NSArray,dict,字典
来源: https://www.cnblogs.com/FdWzy/p/14037694.html