/*
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创建字典,数组
最新推荐文章于 2024-05-13 21:30:50 发布