0
0
Swiftprogramming~15 mins

Explicit type annotation in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Explicit type annotation
📖 Scenario: You are organizing a small library system where you keep track of the number of books available in different genres.
🎯 Goal: You will create variables with explicit type annotations to store the number of books in each genre, then calculate the total number of books.
📋 What You'll Learn
Create variables with explicit type annotations for book counts
Use integer type Int for the counts
Calculate the total number of books using these variables
Print the total number of books
💡 Why This Matters
🌍 Real World
Explicit type annotations help programmers clearly define what kind of data each variable holds, which reduces mistakes and makes code easier to read and maintain.
💼 Career
Many software development jobs require writing clear and type-safe code. Understanding explicit type annotations is essential for working with Swift and other typed languages.
Progress0 / 4 steps
1
Create book count variables with explicit type annotation
Create three variables with explicit type annotation Int named fictionCount, historyCount, and scienceCount. Set their values to 12, 8, and 15 respectively.
Swift
Need a hint?

Use var variableName: Int = value to declare variables with explicit type annotation.

2
Create a variable to hold the total count
Create a variable called totalBooks with explicit type annotation Int and set it to 0.
Swift
Need a hint?

Remember to specify the type Int explicitly when declaring totalBooks.

3
Calculate the total number of books
Add the values of fictionCount, historyCount, and scienceCount and assign the result to totalBooks.
Swift
Need a hint?

Use the + operator to add the counts and assign to totalBooks.

4
Print the total number of books
Write a print statement to display the value of totalBooks.
Swift
Need a hint?

Use print(totalBooks) to show the total number of books.