OC创建字典,数组

/*
 4 创建大量字典
 传入两个数组,key和obj,数组元素个数未知。创建大量字典,字典的个数与key和obj中元素较少的一个数组的元素个数相同。key中第i个元素和obj中第i个元素组成键值对。所有字典添加到一个可变数组中,返回数组的地址。
 */

+ (NSArray *)createDictionariesWithKeys:(NSArray *)key andObjs:(NSArray *)obj
//{
//    NSInteger count = 0;
//    //创建一个临时数组保存字典
//    //NSMutableArray * array = [NSMutableArray array];
//    //如果没有 不是array 必须初始化动态数组
//    NSMutableArray * array = [[NSMutableArray alloc]init];
//    if ([key count] > [obj count]) {
//        count = [obj count];
//    }
//    else
//    {
//        count = [key count];
//    }
//    for (NSInteger i = 0; i < count; i++) {
//        NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithObject:obj[i] forKey:key[i]];
//        [array addObject:dic];
//    }
//    return [array copy];
//}

{
    NSMutableArray * ary = [[NSMutableArray alloc]init];
    NSInteger count = 0;
    if ([key count] > [obj count]) {
        count = [obj count];
    }
    else
    {
        count = [key count];
    }
    for (NSInteger i = 0; i < count; i++) {
       // NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithObject:obj[i] forKey:key[i]];
        //NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithObject:obj[i] forKey:key[i]];
        //这里方法写错了!!!错误!
        //NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithObject:<#(id)#> forKey:<#(id<NSCopying>)#>]
        NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithObject:obj[i] forKey:key[i]];
        [ary addObject:dic];
    }
    return ary;
}
//{
//    NSMutableArray * array = [NSMutableArray array];
//    NSInteger count = 0;
//    if ([key count] > [obj count]) {
//        count = [obj count];
//    }
//    else
//    {
//        count = [key count];
//    }
//    for (NSInteger i = 0; i < count; i++) {
//        NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithObjects:obj[i] forKeys:key[i]];
//        [array addObject:dic];
//    }
//    return [array copy];
//}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值