CRUD OOP todo list, Xcode-Playground.
Tags: beginners, swift
Published: 2020-09-26 19:01:24

The most common and basic way to start learning a new programing language is creating a simple application, in this case `Todo List` 
Any developer can build one, different ways-methods to create an efficient way to keep your task on the line.

Let's use `Swift` and `Xcode` to accomplish it.

> ##### _Todo list es el mas comun y basico procedimiento para  aprender a programar. Crearemos un todo list usando OOP._


- Let's build a Todo class and the way, where to contain each todo

```Swift
class Todo {
  var todos: [String] = []
}
```

- Let's build a method to add a todo into the list

 ```Swift
class Todo {
  func addTodo(todo: String) {
    todos.append(todo)
  }
}
```

### We have a todo list..!
Now what we can do after the task is done. Delete it? Check it as done?

Let's delete when the user inputs the number of the list `index`
> ##### _Ahora implementaremos como borrar y poner un check en cada tarea de la lista. Primero, como borrar un todo usando us index o el nombre._

```Swift
func deleteTodoByIndex(index: Int) -> String? {
   if todos.count == 0 {
      return nil
   }
   return todos.remove(at: index)
}
```

What is the user inputs to delete by the todo name!
```Swift
func deleteTodoByName(todo: String) {
   if todos.count == 0 {
      print("List is empty")
   }
   if let index = todos.firstIndex(of: todo) {
      todos.remove(at: index)
   }
}
```

Great! We know how to delete by name and index each todo, now let's not delete it but check it when is completed
> ##### _Ahora vamos a poner un check por cada todo. El boolean es la accion del usuario en checkear o no el todo._

```Swift
// MARK: - This method takes to params { isChecked: YES || NO, todo name } 
func isChecked(isChecked: Bool, todoToCheck: String) {
    var isChecked = isChecked
    todos.forEach { todo in
       if todoToCheck == todo {
          isChecked = true
       }
    }
    if isChecked == true {
       print("(x) \(todoToCheck)")
    } else {
       print("( ) \(todoToCheck)")
    }
 }
```
> `The boolean YES || NOT toggles depending on the user input...`

Let's check the todo by the index
> ##### _Ahora usaremos el index y el nombre del todo._
```Swift
func isTodoIndexChecked(todoIndex: Int) {
   for index in 0..<todos.count {
      if index == todoIndex {
         print("(x) \(todos[todoIndex])")
         break
      }
   }
}
```
> `This method only checks the index input...`

Now let's check the todo by the name

```Swift
func isTodoNameChecked(todoName: String) {
   todos.forEach { todo in
      if todo == todoName {
         print("(x) \(todoName)")
         break
      }
   }
}
```
Now let's implement search on the list, depending on the position of the todo we have to considerate time and space 

As UX the list position will display todos for the user but what happens behind the user eyes? 

> ##### _Ahora tenemos que implementar la busqueda de cada todo. Tenemos que considerar dependiendo del tamaño de lista, su espacio y tiempo. Vamos a visualizar todos usando la ventana de debugger_

Let's display the todos in the debugger window, `first, all & last` 
```Swift
func displayFirstTodo() {
   if let firstTodo = todos.first {
      print(firstTodo)
   }
}
    
func displayLastTodo() {
   if let lastTodo = todos.last {
      print(lastTodo)
   }
}
    
func displayAllTodos() {
    todos.forEach { todo in
      print("Todo: \(todo)")
   }
}
```
We have three methods for basic display. Now let's combine all this three in one method.

> ##### _Tenemos tres diferentes metodos para visualizar nuestros todos. Ahora vamos a combinarlos un un solo metodo._

```Swift
func displayTodos(which todo: String) {
   switch todo {
      case "first":
         displayFirstTodo()
      case "all":
         displayAllTodos()
      case "last":
         displayLastTodo()
      default:
         break
   }
}
```
`The method takes a param that decides what need to be done. For this case we only display "first", "all" and "last"`

Great! Now to complete the **CRUD** on this todo list, we are going to implement how to update a todo, using the index or the name.

> ##### _Bien, ahora para completar esta lista tenemos que actualizar cada todo, usando su index o nombre._

Let's updated using the index
```Swift
func updateTodoByIndex(index: Int, newTodo: String) {
   for oldTodo in 0..<todos.count {
      if oldTodo == index {
         todos[oldTodo] = newTodo
      } else {
         print("Index: \(index) does not exist in the list")
      }
   }
}
```
Now by the name
```Swift
func updateTodoByName(oldTodo: String, newTodo: String) {
   for i in 0..<todos.count {
      if todos[i] == oldTodo {
         todos[i] = newTodo
      } else {
         print("Todo: \(oldTodo) does not exist in the list.")
      }
   }
}
```
This is great..! We have a **CRUD** todo list. There is many ways to create todo list using any programming language.
Maybe this is not as good it should be but to give an idea on how to implement a basic todo list.

If you like or hate the post, I will appreciate any constructive feedback.
In the future I am going to implement a todo list in a real iOS application on programmatically, storyboard or SwiftUI method, using **CRUD** with fake, local or remote database.

> ##### _Hay muchas maneras de escribir un todo list, quizas esta no es la mejor manera pero la idea es como empezar con algo muy basico para entender como implementar un todo list._

`Cheers.. 🍺 && Happy Coding`