Creating a swift command line to-do list app

I’m new to Swift, but here goes nothing. My first to-do list app in a new language! I’m going to walk you through the steps I’m taking to make it happen — I will for sure be working closely with documentation at the same time. So onto creating a basic to-do list app using the swift command line, arrays, and some functions. All of this is going to be done in the Xcode playground. If you don't have Xcode downloaded — use this link to download and install the necessary files to follow along. Also, I would like to have the ability to sort the list. For this tutorial, I’m not going to use views, just straight out playground code.

To get started, I’m going to create an empty array called to-do-list…how do I create an array?

There are a few ways to create an array.

let Numbers = [7,8,9,10,13,14]

or you can set string elements

let streets = ["Albemarle", "Brandywine", "Chesapeake"]

both of these dynamically infer the type of element you are putting in the array, but since this is a to-do list app, we need to start with an empty array, so there’s nothing to infer.

For clarity purposes and typing good code, I’m going to use the full type name. I am trying to practice leveraging Swift’s strong type language capabilities, which I am not familiar with through Rails and Javascript.

So to create my to-do-list, I’m going to create an empty array called toDoList using the full type name.

var toDolist: Array<String> = Array()=> []

Now we have the empty array, so it’s time to create our first task. Since each task will be a constant, I intentionally used let instead of var when defining the variable.

let task1: String = “Brush Teeth”

Now append the task to the array.

toDolist.append(task1)
=> ["Brush Teeth"]

So we have one task in there, what if we wanted to append multiple more at once? We can use the contentsOf method provided for you in the append function

toDolist.append(contentsOf:[task1, task2, task3])
print(toDolist)
=> ["Brush Teeth", "Comb Hair", "Call Holly"]

Let’s create a simple function to add a new task to the todo list array.

func newTask(task: String) -> Array<String>  {toDolist.append(task)return toDolist}print(newTask(task: "Make Breakfast"))
=> ["Brush Teeth", "Comb Hair", "Call Holly", "Make Breakfast"]

I was pretty happy when this started to work…I created a function called new task and set the parameters of the task to a string…I set the return value to an Array of Strings. This was my first time successfully setting a return value in a function in Swift.

I then appended the task to the toDoList and returned the toDoList array. Initially, I didn’t have the return statement, I just tried to append, and the function didn’t run. This makes sense because I told the function I would return an array, and if I’m appending, I’m not returning anything…

Now I have a new issue, each time I run the function the last item in the array is not saving and being overwritten. How do I commit the array to memory so that I can continuously add items from the function? It turns out that was a format issue with how I was calling and printing the function within the same line — the better way to test out the function would be as follows

newTask(task: "Eat Breakfast")
newTask(task: "Cook Dinner")
print(toDolist)
=> ["Eat Breakfast", "Cook Dinner"]

Now that I have the function set up I’m going to streamline my playground set up a bit…

var toDolist: Array<String> = Array();func newTask(task: String) -> Array<String>  {toDolist.append(task)return toDolist}newTask(task: "Eat Breakfast")newTask(task: "Cook Dinner")
print(toDolist)

Ok, now let’s say I completed one of the tasks, how can I mark it as complete?

I’m going to create a function that checks to see if the typed task is in the current list — if it is in the delete it, if not, create the task

First, let’s check to find a task

let foundTask = toDolist.firstIndex(of: "Cook Dinner")
=> Optional(1)

First Index automatically sets the index as an optional, so if there is nothing there when searching, it will set the value as nil and therefore not stop the code from running. We can then take the found object and remove it from the to-do list.

toDolist.remove(at: foundTask!) 

I started out by creating a function that takes in a string, locates it within the Array, and then removes it

func taskMachine(task: String) -> Array<String>{var chosenTask: Int = toDoList.firstIndex(of: task) ?? -1toDoList.remove(at: chosenTask)return toDoList}print(taskMachine(task: "Cook Dinner"))

The line below implies that if toDoList does not have the index of task, then set the task to negative 1. Since an array will never be a negative number, so I think, I set -1 as the value

var chosenTask: Int = toDoList.firstIndex(of: task) ?? -1

I then created an if-else statement which implies that if the index number does not equal -1 that means it is, in fact, a string within the array, so take the chosenTask number and remove it from the array

if (chosenTask != -1) {toDoList.remove(at: chosenTask)return toDoList}

else create append the string to the toDoList

else{
toDoList.append(task)
return toDoList
}

So as you can see, here’s the simple to-do list that we made using the Swift Playground! I hope to implement the views soon! But will continue on this project, it seems like a great way to learn the basics of Swift!

Demonstration:

print(toDoList) => []taskMachine(task: "Refry the beans")taskMachine(task: "Take a cold shower")taskMachine(task: "Comb my hair")=> ["Refry the beans", "Take a cold shower", "Comb my hair"]

Here you can see that the tasks have been added to the array, and if we add the same task again

taskMachine(task: “Refry the beans”)
=> ["Take a cold shower", "Comb my hair"]

It gets removed from the array!

And that’s my first Swift post, I hope you enjoyed it

Designer, Developer, Musician.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store