DEV Community

Anis Ali Khan
Anis Ali Khan

Posted on

Tutorial 2: Swift Basics - Variables, Constants, and Data Types

Introduction

Welcome to your first hands-on Swift tutorial! Today, we’re diving into variables, constants, and data types while building something useful (and hilarious) — a Fart Recorder App for iOS. This app will allow users to record, label, and play back their “masterpieces.” 🎤💨

By the end of this tutorial, you’ll understand

• How to declare and use variables (var) and constants (let)
• The difference between Strings, Integers, Doubles, and Booleans
• How to work with arrays and dictionaries
• Basic SwiftUI elements to build a simple UI

Step 1: Create a New SwiftUI Project

  1. Open Xcode.
  2. Select Create a new Xcode project.
  3. Choose App under iOS.
  4. Name it FartRecorder.
  5. Select SwiftUI as the interface and Swift as the language.
  6. Click Create! 🚀

Step 2: Understanding Variables and Constants

In Swift, variables (var) can change, while constants (let) cannot.

var fartCount = 0  // Variable: changes as you record farts
let maxFarts = 10   // Constant: the max number of farts you can record
Enter fullscreen mode Exit fullscreen mode

Since farts are precious audio artifacts, we’ll store them in an array:

var fartCollection: [String] = []  // Array of fart sound file names
Enter fullscreen mode Exit fullscreen mode

Step 3: Designing the UI

Modify ContentView.swift to create a basic UI.

import SwiftUI
import AVFoundation

struct ContentView: View {
    @State private var fartCount = 0
    @State private var fartCollection: [String] = []
    @State private var isRecording = false
    var body: some View {
        VStack {
            Text("Fart Recorder")
                .font(.largeTitle)
                .padding()

            Button(action: recordFart) {
                Text(isRecording ? "Recording..." : "Record a Fart")
                    .padding()
                    .background(isRecording ? Color.red : Color.green)
                    .foregroundColor(.white)
                    .clipShape(Capsule())
            }

            Text("Farts Recorded: \(fartCount)")
                .font(.title)
                .padding()

            List(fartCollection, id: \..self) { fart in
                Button(fart) {
                    playFart(fartName: fart)
                }
            }
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Implementing the Recording Logic

Recording (Pretend Mode)

Since recording farts is an art, we’ll simulate the process:

func recordFart() {
    guard fartCount < 10 else { return }  // Prevents more than 10 farts
    let fartName = "Fart_\(fartCount + 1)"
    fartCollection.append(fartName)
    fartCount += 1
    print("Saved: \(fartName)")
}
Enter fullscreen mode Exit fullscreen mode

Playing the Fart Sounds

func playFart(fartName: String) {
    print("Playing \(fartName) 💨")
}
Enter fullscreen mode Exit fullscreen mode

Step 5: Run and Test

  1. Click Run (Cmd + R) in Xcode.
  2. Tap Record a Fart (it will simulate recording).
  3. Tap on a recorded fart to “play” it.
  4. Keep recording until you reach 10 farts.

Next Steps

✅ You built a Fart Recorder! You now understand:
• Variables (var) and Constants (let)
• Basic Data Types: String, Int, Array
• Using SwiftUI Buttons and Lists

Coming up next: Swift Control Flow: Loops, Conditionals, and Optionals

AWS Industries LIVE! Stream

Business benefits of the cloud

See how the cloud delivers real business value across industries on Industries LIVE! New episodes streaming every week.

Learn More

Top comments (0)

Retry later