Introducci贸n a Swift y XCode
Swift y el ecosistema Apple
Instalaci贸n y configuraci贸n de XCode
Quiz: Introducci贸n a Swift y XCode
Fundamentos de programaci贸n iOS
Variables, constantes y tipos de datos b谩sicos
Condicionales
Funciones
Tipos de datos opcionales
Arreglos: append, insert, como crearlas
Arreglos: validar, editar y agregar una lista dentro de otra lista
Conjuntos: como agregar o eliminar elementos
Conjuntos: principales operaciones entre conjuntos
Diccionarios
Recorridos parte 1: while
Recorridos parte 2: for
Recorridos parte 3: los retos
Quiz: Fundamentos de programaci贸n iOS
POO en iOS
Programaci贸n orientada a objetos en iOS
Structs
Clases y herencia
Enums
Protocolos
Funciones de arreglos (filter, map, reduce)
Funciones de arreglos parte 2 (filter, map, reduce): playground
Quiz: POO en iOS
Manejo de errores y programaci贸n segura
Manejo de errores y programaci贸n segura
Propagaci贸n de errores
Do, try, catch
Quiz: Manejo de errores y programaci贸n segura
Programaci贸n en el ecosistema de Apple
Siguientes pasos para el desarrollo en iOS
You don't have access to this class
Keep learning! Join and start boosting your career
In the world of software development, it is essential to learn how to handle more complex data types in order to represent entities effectively. For example, in an online store, a product must include information such as name, price, stock, description, reviews, among others. For this purpose, Swift offers a powerful data structure known as dictionaries or maps, which allows us to model these entities in an organized way, assigning a "key" to each corresponding value.
To start working with maps, the first thing you need to learn is how to declare them. For example, if you want to create a map in Swift, the declaration would be as follows:
var map: [String: Any] = [ "key1": "value1", "key2": "value2", "key3": 12 ]
Strings
, but the values can be of different types. If the values are varied, Any
is used to define as the value type.Any
.Let's imagine you want to model a product. Here's how you can do it using a map:
var product: [String: Any] = [ "id": 101, "name": "Lenovo PC", "price": 1000, "stock": 10, "promo": true ]
To access a value in the dictionary, you use its key. Also, you can use as
to make sure that the type is the right one:
if let ProductName = product["name"] as? String { print("ProductName: \(productName)") }
Modifying the values is equally simple:
product["stock"] = 1 product["price"] = 2000
Add data:
product["rating"] = 5
Removing data is straightforward:
Using removeValue(forKey:)
function:
product.removeValue(forKey: "promotion")
Assigning nil
directly to a key:
product["stock"] = nil
Removing all values:
product.removeAll()
Next, you are given a small challenge to reinforce your learning:
Write a function that calculates the number of inhabitants for a given country and returns the following statement: "In [country name] there are [number of inhabitants] inhabitants." In case no information is available, return "I don't have enough data for an enlightening answer."
Here's an example initial setup:
let inhabitantsCountry: [String: Int] = [ "Argentina": 45195777, "Mexico": 126190788, "Brazil": 211000000 ] func getInhabitantsCountry(country: String) -> String { // Function code }
Contributions 7
Questions 0
Want to see more contributions, questions and answers from the community?