Arreglos y diccionarios en Swift
Colecciones de tipo valor con sintaxis literal.
// Arrays and dictionaries are value types with literal syntax.
var fleet = ["Kestrel", "Sable"]
fleet.append("Petrel")
fleet.insert("Brant", at: 0)
print("\(fleet.count) boats, first away: \(fleet[0])")
var sailNumbers: [String: Int] = ["Kestrel": 207]
sailNumbers["Petrel"] = 415
sailNumbers["Sable"] = 88
// Updating returns the old value, Optional of course.
let old = sailNumbers.updateValue(89, forKey: "Sable")
print("Sable was #\(old ?? 0), now #\(sailNumbers["Sable"] ?? 0)")
print(fleet.contains("Osprey"))
Cómo funciona
appendeinsert(_:at:)hacen crecer un arreglo.- La asignación por subíndice escribe una clave de diccionario.
updateValuedevuelve el valor viejo — Optional, claro.
Palabras clave y builtins usados aquí
IntStringcontainscountinsertletprintvar
El intento, en números
- Líneas
- 14
- Caracteres a escribir
- 518
- Tokens
- 135
- Ritmo de tres estrellas
- 70 tpm
Al ritmo de tres estrellas de 70 tokens por minuto, este intento toma unos 116 segundos.
Paso 1 de 3 en Colecciones; paso 13 de 27 en Fundamentos del lenguaje.