Convert NSArray to NSDictionary

后端 未结 6 1992
清酒与你
清酒与你 2020-12-02 22:13

How can I convert an NSArray to an NSDictionary, using an int field of the array\'s objects as key for the NSDictionary?

6条回答
  •  暖寄归人
    2020-12-02 22:59

    This is an example of creating a NSMutableDictionary from the employee list NSMutableArray:

     NSMutableArray *emloyees = [[NSMutableArray alloc]initWithObjects:@"saman",@"Ruchira",@"Rukshan",@"ishan",@"Harsha",@"Ghihan",@"Lakmali",@"Dasuni", nil];
    
     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
     for (NSString *word in emloyees) {
     NSString *firstLetter = [[word substringToIndex:1] uppercaseString];
     letterList  = [dict objectForKey:firstLetter];
    
    if (!letterList) {
        letterList = [NSMutableArray array];
        [dict setObject:letterList forKey:firstLetter];
    }
    [letterList addObject:word];}  NSLog(@"dic %@",dict);
    

提交回复
热议问题