【问题标题】:Two NSArrays combine to a NSDictionary with alternating values两个 NSArray 组合成一个具有交替值的 NSDictionary
【发布时间】:2020-06-02 11:35:14
【问题描述】:

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

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

我希望它们在字典中组合后变成这样

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

任何反馈将不胜感激!

【问题讨论】:

标签: arrays objective-c dictionary data-structures concatenation


【解决方案1】:

数组

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

我假设您在NSArray 中有它们并且您知道NSNumberObjective-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:
    猜你喜欢
    • 2018-04-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2015-03-07
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多