两个NSArrays组合成具有交替值的NSDictionary [英] Two NSArrays combine to a NSDictionary with alternating values

查看:92
本文介绍了两个NSArrays组合成具有交替值的NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我是Objective-c的新手,请原谅我的无知。所以基本上这就是我想发生的事情。我有两个数字数组

  Array1:(1,3,5); 
Array2:(2,4,6);

我希望它们在词典中组合后像这样

 字典:{ 1:2 ,: 3:4, 5:6} 

任何反馈将不胜感激!

解决方案

数组




我有两个数字数组, Array1:(1、3、5)& Array2:(2,4,6)


我假设您有它们放在 NSArray 中,并且您知道 NSNumber & Objective-C文字。换句话说,您将:

  NSArray * keys = @ [@@@@ 3, @ 5]; // NSNumber数组
NSArray * objects = @ [@ 2,@ 4,@ 6]; // NSNumber数组




dictionary:{ 1: 2 ,: 3:4, 5:6}


我认为这意味着:

  @ {
@ dictionary:@ {
@ 1:@ 2,
@ 3:@ 4,
@ 5:@ 6
}
}



步骤1-字符串化字符串



  NSArray *键= @ [@@,@ 3,@ 5]; 
NSArray *对象= @ [@ 2,@ 4,@ 6];

NSMutableArray * stringifiedKeys = [NSMutableArray arrayWithCapacity:keys.count];
(NSNumber *键中的键){
[stringifiedKeys addObject:key.stringValue];
}



步骤2-创建字典



dictionaryWithObjects:forKeys:

  +( instancetype)dictionaryWithObjects:(NSArray< ObjectType> *)对象
forKeys:(NSArray< id< NSCopying> * *)keys;

您可以通过以下方式使用它:



< preclass = lang-c prettyprint-override> NSArray * keys = @ [@ 1,@ 3,@ 5];
NSArray *对象= @ [@ 2,@ 4,@ 6];

NSMutableArray * stringifiedKeys = [NSMutableArray arrayWithCapacity:keys.count];
(NSNumber *键中的键){
[stringifiedKeys addObject:key.stringValue];
}

NSDictionary * dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:stringifiedKeys];



步骤3-将其包装在字典中



< preclass = lang-c prettyprint-override> NSArray * keys = @ [@ 1,@ 3,@ 5];
NSArray *对象= @ [@ 2,@ 4,@ 6];

NSMutableArray * stringifiedKeys = [NSMutableArray arrayWithCapacity:keys.count];
(NSNumber *键中的键){
[stringifiedKeys addObject:key.stringValue];
}

NSDictionary * dictionary = [NSDictionary dictionaryWithObjects:objects
forKeys:stringifiedKeys];
NSDictionary * result = @ {@ dictionary:字典};

NSLog(@%@,结果);

结果:

  {
字典= {
1 = 2;
3 = 4;
5 = 6;
};
}



手动



<课前= lang-c prettyprint-override> NSArray * keys = @ [@ 1,@ 3,@ 5];
NSArray *对象= @ [@ 2,@ 4,@ 6];

// Mimick dictionaryWithObjects:forKeys:行为
if(objects.count!= keys.count){
NSString * reason = [NSString stringWithFormat:@对象数( %lu)与键计数(%lu),(无符号长)objects.count,(无符号长)keys.count]不同;

@throw [NSException exceptionWithName:NSInvalidArgumentException
原因:原因
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] =对象;
}

NSDictionary * result = @ {@ dictionary:内部};



脚注



由于我对Objective-C陌生,我确实有意避免:




  • Blocks&比较安全的枚举方式

  • 轻量级仿制药

  • 可空性


Hi I am very new to Objective-c please excuse my ignorance. So basically this is what I want to happened. I have two arrays of numbers

Array1: (1,3,5);
Array2: (2,4,6);

and I want them to be like this after combining them in a dictionary

"dictionary":{"1":2,: "3":4, "5":6}

Any feedback will be appreciated!

解决方案

Arrays

I have two arrays of numbers Array1: (1,3,5) & Array2: (2,4,6)

I assume that you have them in NSArray and that you're aware of NSNumber & Objective-C Literals. In other words, you have:

NSArray *keys    = @[@1, @3, @5]; // Array of NSNumber
NSArray *objects = @[@2, @4, @6]; // Array of NSNumber

"dictionary":{"1":2,: "3":4, "5":6}

I assume that it means:

@{
    @"dictionary": @{
        @"1": @2,
        @"3": @4,
        @"5": @6
    }
}

Step 1 - Stringify 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];
}

Step 2 - Create a dictionary

dictionaryWithObjects:forKeys::

+ (instancetype)dictionaryWithObjects:(NSArray<ObjectType> *)objects 
                              forKeys:(NSArray<id<NSCopying>> *)keys;

You can use it in this way:

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];

Step 3 - Wrap it in a dictionary

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);

Result:

{
    dictionary = {
        1 = 2;
        3 = 4;
        5 = 6;
    };
}

Manually

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 };

Footnotes

Because of I am very new to Objective-C, I did intentionally avoid:

  • Blocks & safer ways to enumerate
  • Lightweight generics
  • Nullability stuff

这篇关于两个NSArrays组合成具有交替值的NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆