Code Like A Girl

Welcome to Code Like A Girl, a space that celebrates redefining society's perceptions of women in technology. Share your story with us!

Follow publication

Getting started with Swift …

--

Swift is Apple’s powerful and intuitive programming language, which is one of the best choices for developing iOS applications and releasing in AppStore. With it’s clean syntax and performance optimisations, Swift makes app development easier and accessible to beginners while offering advanced capabilities for experienced developers.

In this blog, you’ll be building a simple to-do list app in Swift using Xcode and SwiftUI. By the end, you’ll have a functional app that allows users to add and delete tasks, which you can further develop as per your interest…

Step 1 : Setting Up the Project

Before starting to code, we need to set up our development environment.

1. Install Xcode

Download and install Xcode, Apple’s official IDE, from the Mac App Store. Xcode includes everything you need to develop iOS apps, including the Swift compiler and iOS Simulator.

2. Create a New Project

  • Open Xcode and select “Create a new Xcode project”.
  • Choose “App” under iOS and click “Next”.
  • Name your project (e.g., “ToDoApp”) and set the interface to SwiftUI.
  • Click “Next”, then choose a location to save your project.

The tree or project structure looks as shown,

Step 2 : Understanding SwiftUI Basics

SwiftUI is a declarative framework that makes UI design intuitive. Instead of writing complex UI code, you describe how the UI should look and behave. BTW, Xcode provides a live preview of SwiftUI code. Click the Resume button in the canvas to see changes instantly. Here’s a simple SwiftUI Text View :

Step 3 : Creating a To-Do List App

Let’s build a basic to-do list app that allows users to add and delete tasks.

1. Define the Task Model

We need a data model to represent tasks. The Identifiable protocol allows SwiftUI to track each task uniquely.

2. Create the Main View

Let’s create the UI with a text field to enter tasks and a list to display them.

Key Features in This Code:

  • @State is used to store the list of tasks and the new task input.
  • TextField allows users to enter a task.
  • Button triggers the addTask( ) function to add a new task.
  • List displays all tasks and supports swipe-to-delete.
  • .onDelete() enables deleting tasks with a swipe gesture.

Step 4 : Running the App

  1. Click Run (▶) in Xcode to launch the app in the iOS Simulator.
  2. Enter a task in the text field and tap the ➕ button to add it.
  3. Swipe left on a task to delete it.
iPhone Simulator

Step 5: Enhancing the App

Now that we have a basic working app, here are some ideas to improve it :

  • Persistent Storage : Use UserDefaults or CoreData to save tasks.
  • Custom UI : Modify colours, fonts, and animations.
  • Cloud Sync : Sync tasks using Firebase or iCloud.

In order to enhance the app,

1. Animations for Adding & Deleting Tasks

SwiftUI makes animations easy. We can animate task additions and deletions using withAnimation.

Modify the addTask( ) and deleteTask( ) functions in ContentView.swift. After this change, the Tasks will smoothly appear and disappear when added or deleted.

2. Dark Mode Support

SwiftUI automatically supports Dark Mode, but we can customize it.

In ContentView.swift, modify List and HStack to support both Light and Dark modes. Through this change, the app adapts to Dark Mode when enabled in iPhone settings providing an option for users.

3. Persistent Storage with UserDefaults

To save tasks, you can use UserDefaults, a lightweight storage option.

Step 1 : Modify the Task Model

Convert tasks into a Codable array so it can be stored in UserDefaults:

Step 2 : Save & Load Tasks

Modify ContentView.swift to save tasks whenever they are added or deleted. With this edit, the app will now remember tasks even after closing and reopening or you can use cache …

Conclusion

Swift stands out from other programming languages due to its speed, safety and modern syntax. Swift is faster because it compiles directly to machine code, eliminating the overhead of a virtual machine. compared to Python. Swift offers a stronger type safety and better performance while still maintaining an easy-to-read syntax. Unlike C++, Swift handles memory management automatically using ARC (Automatic Reference Counting), reducing memory leaks and making development smoother. Additionally, SwiftUI simplifies UI development, allowing developers to create stunning iOS and macOS apps with minimal code. With Apple’s continuous enhancements or improvements, it remains as the best choice for iOS development, ensuring a seamless, efficient and future-proof experience.

Building a simple Swift app is easier than you think, with SwiftUI and Xcode, you can quickly develop functional and visually appealing iOS applications. This is a simple to-do list app which is just the beginning phase — keep experimenting and developing considering this as a template and soon you’ll be able to build much more complex projects.

Thanks for reading, clap if you find it helpful!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Published in Code Like A Girl

Welcome to Code Like A Girl, a space that celebrates redefining society's perceptions of women in technology. Share your story with us!

Written by Indhu

I talk about Coding | Philosophy | Tech | Life | Mindfulness and anything I feel worth sharing

No responses yet

Write a response