Buscar..


Crear

NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];

o

NSArray *keys = [NSArray arrayWithObjects:@"key1", @"key2", nil];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", nil];
NSDictionary *dictionary = [NSDictionary dictionaryWithObjects:objects 
                                                       forKeys:keys];

o usando la sintaxis literal apropiada

NSDictionary *dict = @{@"key": @"value", @"nextKey": @"nextValue"};

NSDictionary to NSArray

NSDictionary *myDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];  
NSArray *copiedArray = myDictionary.copy; 

Obtener llaves:

NSArray *keys = [myDictionary allKeys];

Obtener valores:

NSArray *values = [myDictionary allValues];

NSDictionary a NSData

NSDictionary *myDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];  
NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];

Ruta de reserva:

NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];

NSDictionary a JSON

NSDictionary *myDictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1", @"value2", @"key2", nil];   

NSMutableDictionary *mutableDictionary = [myDictionary mutableCopy];
NSData *data = [NSJSONSerialization dataWithJSONObject:myDictionary options:NSJSONWritingPrettyPrinted error:nil];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Enumeración basada en bloques

La enumeración de diccionarios le permite ejecutar un bloque de código en cada par de clave-valor del diccionario utilizando el método enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block

Ejemplo:

NSDictionary stockSymbolsDictionary = @{
                                         @"AAPL": @"Apple",
                                         @"GOOGL": @"Alphabet",
                                         @"MSFT": @"Microsoft",
                                         @"AMZN": @"Amazon"
                                       };
NSLog(@"Printing contents of dictionary via enumeration");
[stockSymbolsDictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    NSLog(@"Key: %@, Value: %@", key, obj);
}];

Enumeración rápida

NSDictionary se puede enumerar utilizando la enumeración rápida, al igual que otros tipos de colección:

NSDictionary stockSymbolsDictionary = @{
                                     @"AAPL": @"Apple",
                                     @"GOOGL": @"Alphabet",
                                     @"MSFT": @"Microsoft",
                                     @"AMZN": @"Amazon"
                                   };

for (id key in stockSymbolsDictionary)
{
    id value = dictionary[key];
    NSLog(@"Key: %@, Value: %@", key, value);
}

Debido a que NSDictionary está intrínsecamente desordenado, el orden de las claves que en el ciclo for no está garantizado.



Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow