如何使用数组对象的int字段作为NSArray的键,将NSDictionary转换为NSDictionary

最佳答案

- (NSDictionary *) indexKeyedDictionaryFromArray:(NSArray *)array
{
  id objectInstance;
  NSUInteger indexKey = 0U;

  NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];
  for (objectInstance in array)
    [mutableDictionary setObject:objectInstance forKey:[NSNumber numberWithUnsignedInt:indexKey++]];

  return (NSDictionary *)[mutableDictionary autorelease];
}

07-28 06:22