NSDictionary的遍历

在NSDictionary里用objectAtIndex方法不行,报错,查得此招:

Enumerating all the Keys and Values

Sometime, you need to iterate over all the key/value pairs in a dictionary. To do this, you use the method -allKeys to retrieve an array of all the keys in the dictionary; this array contains all the keys, in no particular (ie random) order. You can then cycle over this array, and for each key retrieve its value. The following example prints out all the key-values in a dictionary:

void describeDictionary (NSDictionary *dict)

  NSArray *keys;
  int i, count;
  id key, value;

  keys = [dict allKeys];
  count = [keys count];
  for (i = 0; i < count; i++)
  {
    key = [keys objectAtIndex: i];
    value = [dict objectForKey: key];
    NSLog (@"Key: %@ for value: %@", key, value);
  }
}

你可能感兴趣的:(IE,Random,each,Dictionary)