【问题标题】:How to retrieve values from two Mutablearrays and store them in MutableDictionary with keys as an array?如何从两个 Mutablearray 中检索值并将它们存储在 MutableDictionary 中,并将键作为数组?
【发布时间】:2013-09-14 06:06:29
【问题描述】:

我有 3 个 NSMutableArrays k,names,numbers

k 数组包含 {a,b,c,d,e,.....}

names 数组包含 {apple,bag,banana,car,cat,dall,elephant,.....}

numbers 数组包含 {100,200,300,400,500,600,700,...}

这里所有的 3 个数组都是动态的。

我想将 names[],numbers[] 添加到 NSMutableDictionary,其中 k[] 作为键数组..

当我输入字典时,我的输出应该是这样的

一个

苹果 100

b

包200

香蕉 300

c

汽车 400

猫 500

d

600 美元

e

大象700

有人可以帮助我吗? 谢谢。

【问题讨论】:

  • 所有数组的长度是否相等?名称数组是否已经排序?

标签: ios objective-c arrays nsmutabledictionary


【解决方案1】:
NSMutableDictionary *result = [NSMutableDictionary new];
for(int i = 0; i<k.count; i++){
    unichar letter = [k objectAtIndex:i]; //I'm assuming you have chars in your k array,            
                                           // if not, you have to format it here
    NSString* name = [names objectAtIndex:i];
    if([[name characterAtIndex:0] isEqual:letter]){
        NSString *objectToInsert = [NSString stringWithFormat:@"%@: %@", @"name", [numbers objectAtIndex:i]];
        [result setObject:objectToInsert forKey:letter];
    }
}

您可能可以对此进行更多优化,但它应该可以工作 ;)

【讨论】:

    【解决方案2】:

    试试下面的代码

    NSMutableDictionary *mydic = [[NSMutableDictionary alloc] init];
    
    For(int k = 0; k < firstArray.count ; k++)
    {
      NSPredicate *pred =[NSPredicate predicateWithFormat:@"SELF beginswith[c] %@", [firstArray objectAtIndex:k]];
      NSArray *filteredArr = [MySecodArray filteredArrayUsingPredicate:pred];
    
      NSString *myString = @"";
      for (int t = 0 ; t < filteredArr.count ; t ++)
      {
        myString = [MyThirdArray objectAtIndex:[MySecodArray indexOfObject:[filteredArr  objectAtIndex:t]]];
        myString = [[filteredArr  objectAtIndex:t] stringByAppendingString:myString];
        [mydic setValue: myString forKey:[firstArray objectAtIndex:k]];
    
        myString = @"";   
      }
    }
    NSLog(@"%@", myDic);
    

    【讨论】:

      【解决方案3】:

      你可以这样做,我假设 nameArry 和 NumberArray 计数相同-->

      试试这个代码

      NSSMutableDictionary *finalDictionary = [[NSSmutableDictionary alloc]init];
      for(int i=0;i<k.count;i++)
      {
         NSString *alphaBet = [k objectAtIndex:i];
      
         //initialize array here 
      
         NSMutableArray *insideDictArray = [[NSMutableArray alloc]init];
      
         for(int j=0;j<nameArray.count;j++)
         {
           NSString *name = [nameArray objectAtIndex:j];
           if([name hasPreffix:alphabet])
           {
             //get Number String
      
             NSString *numberForName = [numberArray objectAtIndex:j];
      
             //Now Concate Number and Name
      
             NSString *concateString = [name stringByAppendingFormat:@"
             %@",numberForName]; 
      
             //Put this in Array we just intialized outside of secondLoop
      
            [insideDictArray addObject:concateString];        
           }
         }
         //Now Add array to finalDictionary
      
         [finalDictionary addObject:insideDictArray forKey:alphabet];
      }
      

      我希望输出会是这样的:

      finalDictionary = 
      {
      a = [aple 100];
      b = [bag 200,banana 300];
      .
      .
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-31
        • 1970-01-01
        • 2020-02-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多