DEV Community

Israel Manzo
Israel Manzo

Posted on

CRUD OOP todo list, Xcode-Playground.

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
class Todo {
  var todos: [String] = []
}
Enter fullscreen mode Exit fullscreen mode
  • Let's build a method to add a todo into the list
class Todo {
  func addTodo(todo: String) {
    todos.append(todo)
  }
}
Enter fullscreen mode Exit fullscreen mode

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.
func deleteTodoByIndex(index: Int) -> String? {
   if todos.count == 0 {
      return nil
   }
   return todos.remove(at: index)
}
Enter fullscreen mode Exit fullscreen mode

What is the user inputs to delete by the todo name!

func deleteTodoByName(todo: String) {
   if todos.count == 0 {
      print("List is empty")
   }
   if let index = todos.firstIndex(of: todo) {
      todos.remove(at: index)
   }
}
Enter fullscreen mode Exit fullscreen mode

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.
// 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)")
    }
 }
Enter fullscreen mode Exit fullscreen mode

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.
func isTodoIndexChecked(todoIndex: Int) {
   for index in 0..<todos.count {
      if index == todoIndex {
         print("(x) \(todos[todoIndex])")
         break
      }
   }
}
Enter fullscreen mode Exit fullscreen mode

This method only checks the index input...

Now let's check the todo by the name

func isTodoNameChecked(todoName: String) {
   todos.forEach { todo in
      if todo == todoName {
         print("(x) \(todoName)")
         break
      }
   }
}
Enter fullscreen mode Exit fullscreen mode

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

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)")
   }
}
Enter fullscreen mode Exit fullscreen mode

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.
func displayTodos(which todo: String) {
   switch todo {
      case "first":
         displayFirstTodo()
      case "all":
         displayAllTodos()
      case "last":
         displayLastTodo()
      default:
         break
   }
}
Enter fullscreen mode Exit fullscreen mode

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

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")
      }
   }
}
Enter fullscreen mode Exit fullscreen mode

Now by the name

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.")
      }
   }
}
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)