Contenidos
Contenido del fichero JSON
Código para crear una aplicación para Apple Watch que lee tareas desde un fichero JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import SwiftUI struct Task: Identifiable, Codable { let id: String let dia: String let tarea: String } struct ContentView: View { @State private var tasks = [Task]() var body: some View { NavigationView { List(tasks) { task in NavigationLink(destination: TaskDetailView(task: task)) { Text(task.tarea) } } .navigationTitle("Tareas") } .onAppear(perform: loadData) } func loadData() { let urlString = "https://www.jesusninoc.com/tareas.json" guard let url = URL(string: urlString) else { print("URL inválida") return } URLSession.shared.dataTask(with: url) { data, _, error in if let error = error { print("Error de red: \(error)") return } guard let data = data else { print("No se recibieron datos") return } do { let decodedData = try JSONDecoder().decode([Task].self, from: data) DispatchQueue.main.async { self.tasks = decodedData } } catch { print("Error al decodificar JSON: \(error)") } }.resume() } } struct TaskDetailView: View { let task: Task var body: some View { Text(task.tarea) .navigationTitle("Detalle de Tarea") } } |