[iOS] TO-DO List app — Swift

MYH
5 min readJul 19, 2021

Building a To-Do list app is probably one of the must-do challenges for Swift noobie, so yes, here is my to-do list app. I didn’t put too much time on designing the UI and other convoluted functions. So basically, it’s just a simple to-do list app. Still, I learnt a lot out of it. And here is the deep dive.

Here is a quick demo on my To-Do app. Basically, you can add new to-do items and also delete them. After you click the item in the list, the Progress Label will auto update itself. Once the Progress Label hits 100%, the medal animation will automatically pop up to congratulate you on finishing all the items. (Apologize in advance if my demo looks a little bit laggy, I’m still rocking with a 2015 MacBook Pro. Impressive, right? lol.)

Here are some key features that I used in my app: UIAlertController, Realm database, UITableViewController, Lottie Animation, Computed properties.

I have already wrote articles on UIAlertController and Lottie Animation. Here’s the links.

So in this article, I’ll focus on sharing how I incorporate Realm and Computed Properties, which in my opinion, is one of the hardest syntax in Swift.

Realm

To add Realm in your Xcode project, we just have to add ‘pod ‘‘RealmSwift’’ to your pod file. For more information, here’s the link to Realm.

I have used CoreData in my last project. Realm is similar to CoreData, but so much faster and controllable in so many ways. Realm can accomplish CRUD in more efficient codes. Take reading data for example, here’s how you read CoreData and update to your tableView.

func loadContacts() {let request : NSFetchRequest<Contact> = Contact.fetchRequest()do {contacts = try context.fetch(request)}catch {print("Error fetching data: \(error)")}tableView.reloadData()}

And here’s how Realm do the same thing but with shorter lines.

func loadItems(){listItem = realm.objects(List.self)tableView.reloadData()}

Some people might prefer CoreData, the other people might feel like Realm is better. There’s no right or wrong. However, I prefer Realm not only because it needs fewer lines of code, but also because it’s more easy to view its database. To me, I feel like it’s harder to view SQLite database since I don’t know any free Application to view the data and its details (So far I’ve tried DBManage, SQLiteManagerTool, Liya, and so on and so forth. But none of it really helps me.) However, in the case of Realm, it has its own Realm Browser (Realm Studio can do the same trick) to help you view your data. (And they are both free to download on its webpage.) It’ll look something like this.

Realm Studio
Realm Browser

Now let’s talk about Computed Properties.

Here’s what I’ve wrote in my project.

private var todoValue : Int {get {guard let number = Int(finishedLabel.text!) else {fatalError("cannot convert text to double")}return number} set {finishedLabel.text = String(newValue)}}

As you can see, this variable doesn’t store any specific value. Instead, it uses getter and setter to retrieve value and update other properties indirectly. It’s a great way to auto update your properties if used wisely. Since I’m not really good at explaining, I’ll paste the official link, which helps me a lot, right here.

I have used Computed Properties to auto update my Progress Label. Here’s how I’ve done it.

func checkForProgress() {var realmObjectFinished = realm.objects(List.self).filter("done == true").countvar realmObjectTotal = realm.objects(List.self).filter("done == true || done == false").countif realmObjectTotal == 0 {todoValue = 0} else {todoValue = Int(realmObjectFinished)*100/Int(realmObjectTotal)}if todoValue == 100 {popupAnimation()} else {animationView.removeFromSuperview()}}

I built filters with NSPredicate to help me count the numbers of the total items and the items that’ve been finished. And I placed this function, checkForProgress(), to my add button’s @IBAction, tableView’s didSelectRowAt() and commit editingStyle() functions so that every time I add, delete or check a to-do item, the Progress Label will update itself. Once the Progress Label hits 100%, the Lottie animation will automatically pop up. And if you deselect one item, which makes it not 100% no more, the animation will automatically been removed from screen and the Progress Label will update to the right value.

And that’s a wrap. I didn’t paste the Github link right here because git has a maximum push limit of 100 megabyte. One of the downsides of using Realm database is that it’s pretty easy to exceed that limit, so I’m still working on that (will probably write an article once I succeed).

--

--