两个 NSArray 组合成一个具有交替值的 NSDictionary
Posted
技术标签:
【中文标题】两个 NSArray 组合成一个具有交替值的 NSDictionary【英文标题】:Two NSArrays combine to a NSDictionary with alternating values 【发布时间】:2020-06-02 11:35:14 【问题描述】:您好,我是 Objective-c 的新手,请原谅我的无知。所以基本上这就是我想要发生的事情。我有两个数字数组
Array1: (1,3,5);
Array2: (2,4,6);
我希望它们在字典中组合后变成这样
"dictionary":"1":2,: "3":4, "5":6
任何反馈将不胜感激!
【问题讨论】:
您只需为此编写一小段代码。这很简单。dictionaryWithObjects:forKeys:
应该做这项工作。 developer.apple.com/documentation/foundation/nsdictionary/…
【参考方案1】:
数组
我有两个数字数组
Array1: (1,3,5)
&Array2: (2,4,6)
我假设您在NSArray
中有它们并且您知道NSNumber
和Objective-C Literals。换句话说,你有:
NSArray *keys = @[@1, @3, @5]; // Array of NSNumber
NSArray *objects = @[@2, @4, @6]; // Array of NSNumber
"字典":"1":2,:"3":4, "5":6
我认为这意味着:
@
@"dictionary": @
@"1": @2,
@"3": @4,
@"5": @6
第 1 步 - 字符串化键
NSArray *keys = @[@1, @3, @5];
NSArray *objects = @[@2, @4, @6];
NSMutableArray *stringifiedKeys = [NSMutableArray arrayWithCapacity:keys.count];
for (NSNumber *key in keys)
[stringifiedKeys addObject:key.stringValue];
第 2 步 - 创建字典
dictionaryWithObjects:forKeys:
:
+ (instancetype)dictionaryWithObjects:(NSArray<ObjectType> *)objects
forKeys:(NSArray<id<NSCopying>> *)keys;
你可以这样使用:
NSArray *keys = @[@1, @3, @5];
NSArray *objects = @[@2, @4, @6];
NSMutableArray *stringifiedKeys = [NSMutableArray arrayWithCapacity:keys.count];
for (NSNumber *key in keys)
[stringifiedKeys addObject:key.stringValue];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:stringifiedKeys];
第 3 步 - 将其包装在字典中
NSArray *keys = @[@1, @3, @5];
NSArray *objects = @[@2, @4, @6];
NSMutableArray *stringifiedKeys = [NSMutableArray arrayWithCapacity:keys.count];
for (NSNumber *key in keys)
[stringifiedKeys addObject:key.stringValue];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:stringifiedKeys];
NSDictionary *result = @ @"dictionary": dictionary ;
NSLog(@"%@", result);
结果:
dictionary =
1 = 2;
3 = 4;
5 = 6;
;
手动
NSArray *keys = @[@1, @3, @5];
NSArray *objects = @[@2, @4, @6];
// Mimick dictionaryWithObjects:forKeys: behavior
if (objects.count != keys.count)
NSString *reason = [NSString stringWithFormat:@"count of objects (%lu) differs from count of keys (%lu)", (unsigned long)objects.count, (unsigned long)keys.count];
@throw [NSException exceptionWithName:NSInvalidArgumentException
reason:reason
userInfo:nil];
NSMutableDictionary *inner = [NSMutableDictionary dictionaryWithCapacity:keys.count];
for (NSUInteger index = 0 ; index < keys.count ; index++)
NSString *key = [keys[index] stringValue];
NSString *object = objects[index];
inner[key] = object;
NSDictionary *result = @ @"dictionary": inner ;
脚注
因为我对 Objective-C 很陌生,所以我故意避免:
块和更安全的枚举方式 轻量级泛型 可以为空的东西【讨论】:
【参考方案2】:您可以使用以下代码进行尝试:
- (NSDictionary *)convertToDicFromKeys:(NSArray *)keys andValues:(NSArray *)values
NSInteger count = MIN(keys.count, values.count);
NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:count];
for (int i = 0; i < count; i++)
dic[keys[i]] = values[i];
return dic;
【讨论】:
嗨。我收到Expected method to read dictionary element not found on object of type 'NSMutableArray
的错误
@jordanzee 不要使用这个,这是错误的,尤其是这个dic[keys[i]] = dic[values[i]]
行。正如拉姆已经告诉你的那样,使用dictionaryWithObjects:forKeys:
。以上是关于两个 NSArray 组合成一个具有交替值的 NSDictionary的主要内容,如果未能解决你的问题,请参考以下文章
SQL:将查询输出更改为具有两个单独的列,而不是具有 2 个值的行