码迷,mamicode.com
首页 > 编程语言 > 详细

OC创建字典,数组

时间:2015-07-26 21:03:47      阅读:720      评论:0      收藏:0      [点我收藏+]

标签:字符串   foundation框架   objective-c   字典   ios开发   

/*
 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];
//}


版权声明:本文为博主原创文章,未经博主允许不得转载。

OC创建字典,数组

标签:字符串   foundation框架   objective-c   字典   ios开发   

原文地址:http://blog.csdn.net/u012701023/article/details/47070523

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!