CUSTOMISED
Expert-led training for your team
Dismiss
Swift Programming Language Example: Building a To-Do List App

3 April 2023

Swift Programming Language Example: Building a To-Do List App

Introduction:

Swift is a powerful and user-friendly programming language that is widely used in iOS, macOS, and watchOS app development. In this guide, we will provide a step-by-step tutorial on how to build a simple to-do list app using Swift programming language. We will cover the basic features of Swift, including data types, control flow, functions, and more.

Section 1: Setting Up the Project

The first step in building our to-do list app is to set up the project in Xcode. Here are the steps to follow:

  1. Open Xcode and click "Create a new Xcode project".
  2. Choose "iOS" as the platform and "Single View App" as the template.
  3. Enter a product name, organization identifier, and choose a language of "Swift".
  4. Click "Next" and choose a location to save the project.
  5. Click "Create" to create the project.

Section 2: Building the User Interface

The next step is to build the user interface for our to-do list app. Here are the steps to follow:

  1. Open the "Main.storyboard" file in the project navigator.
  2. Drag a "Table View" object from the Object Library onto the view controller.
  3. Drag a "Table View Cell" object from the Object Library onto the table view.
  4. Customize the table view cell by adding a label and a checkmark accessory view.
  5. Create a new Swift file called "TodoItem" and define a class for our to-do list item:

swift

class TodoItem { var title: String var completed: Bool init(title: String, completed: Bool = false) { self.title = title self.completed = completed } }

Section 3: Implementing the To-Do List Functionality

Now that we have built the user interface, we can implement the to-do list functionality. Here are the steps to follow:

  1. Create a new Swift file called "TodoList" and define a class for our to-do list:

swift

class TodoList { var items: [TodoItem] init(items: [TodoItem] = []) { self.items = items } func addItem(_ item: TodoItem) { items.append(item) } func removeItem(at index: Int) { items.remove(at: index) } func toggleItemCompleted(at index: Int) { items[index].completed = !items[index].completed } }

  1. Open the view controller file and define a property for our to-do list:

swift

var todoList = TodoList()

  1. Implement the table view data source methods to display the to-do list items:

swift

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return todoList.items.count } func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "TodoItemCell", for: indexPath) let item = todoList.items[indexPath.row] cell.textLabel?.text = item.title cell.accessoryType = item.completed ? .checkmark : .none return cell }

  1. Implement the table view delegate methods to handle selecting and deselecting to-do list items:

swift

 

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

    if editingStyle == .delete {

        // Code to delete the row goes here

    } else if editingStyle == .insert {

        // Code to insert a new row goes here

    } else if editingStyle == .none {

        // Code to handle other editing styles goes here

    }

}

 

  1. Implement the add item functionality by adding a text field and a bar button item to the navigation bar:

swift

 

@IBOutlet weak var newItemTextField: UITextField! @IBAction func addButtonTapped(_ sender: UIBarButtonItem) { guard let title = newItemTextField.text, !title.isEmpty else { return } let item = TodoItem(title: title) todoList.addItem(item) tableView.reloadData() newItemTextField.text = "" }

  1. Implement the delete item functionality by adding a swipe action to the table view cells:

swift

func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? { let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { [weak self] (action, view, completionHandler) in self?.todoList.removeItem(at: indexPath.row) tableView.deleteRows(at: [indexPath], with: .automatic) completionHandler(true) } deleteAction.backgroundColor = .red return UISwipeActionsConfiguration(actions: [deleteAction]) }

Section 4: Testing the App

Now that we have implemented the to-do list functionality, we can test the app to make sure it works as expected. Here are the steps to follow:

  1. Build and run the app in the simulator or on a device.
  2. Add some items to the to-do list using the text field and the "Add" button.
  3. Swipe left on a to-do list item to reveal the "Delete" button and delete some items.
  4. Tap on a to-do list item to toggle its completed status.

Conclusion:

In this guide, we have provided a step-by-step tutorial on how to build a simple to-do list app using Swift programming language. We covered the basic features of Swift, including data types, control flow, functions, and more. By following this guide, you should have a good understanding of how to build a basic iOS app using Swift programming language.

 

We hope you found this guide on Swift Programming Language Example: Building a To-Do List App insightful and valuable. You can learn more on JBI's Swift Training Course.

Go here if you would like to see the Swift Programming Language Official Documentation.

About the author: Craig Hartzel
Craig is a self-confessed geek who loves to play with and write about technology. Craig's especially interested in systems relating to e-commerce, automation, AI and Analytics.

CONTACT
+44 (0)20 8446 7555

[email protected]

SHARE

 

Copyright © 2023 JBI Training. All Rights Reserved.
JB International Training Ltd  -  Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS

Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us

POPULAR

Rust training course                                                                          React training course

Threat modelling training course   Python for data analysts training course

Power BI training course                                   Machine Learning training course

Spring Boot Microservices training course              Terraform training course

Kubernetes training course                                                            C++ training course

Power Automate training course                               Clean Code training course