NSDictionary en Objective-C
@{} empareja claves con valores; el fallo responde nil.
#import <Foundation/Foundation.h>
int main(void) {
@autoreleasepool {
// @{} pairs keys with values; subscripts read and miss to nil.
NSDictionary<NSString *, NSNumber *> *stock = @{
@"Kind of Blue" : @7,
@"Maggot Brain" : @2,
};
NSLog(@"Titles stocked: %lu", (unsigned long)[stock count]);
NSLog(@"Maggot Brain: %@ copies", stock[@"Maggot Brain"]);
NSLog(@"Missing title: %@", stock[@"Pet Sounds"]);
for (NSString *title in [stock allKeys]) {
NSLog(@"- %@", title);
}
}
return 0;
}
Cómo funciona
@{ clave : valor }es el literal; el subíndice lo lee.- Una clave ausente devuelve nil, impreso como (null).
allKeysentrega las claves a un bucle for-in.
Palabras clave y builtins usados aquí
NSDictionaryNSNumberNSStringforinintlongreturnunsignedvoid
El intento, en números
- Líneas
- 19
- Caracteres a escribir
- 484
- Tokens
- 110
- Ritmo de tres estrellas
- 60 tpm
Al ritmo de tres estrellas de 60 tokens por minuto, este intento toma unos 110 segundos.
Paso 2 de 3 en Colecciones; paso 11 de 27 en Fundamentos del lenguaje.