Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
5: Basic Types and Type Inference
I see this specific error constantly when developers move to Swift from languages like JavaScript or Python. You’re writing a simple piece of logic, it looks perfectly fine to the human eye, but the compiler throws a fit. Take a look at this snippet for a shopping cart calculation:
let itemQuantity = 3
let pricePerItem = 12.99
let totalCost = itemQuantity * pricePerItem
// Error: Binary operator '*' cannot be applied to operands of type 'Int' and 'Double'
The 'Binary Operator' Headache
At first glance, this looks like a non-issue. You have an integer (3) and a decimal (12.99). In most languages, the computer just says, "Okay, I'll treat the 3 as a 3.0 and do the math." But Swift is strictly typed. It refuses to guess your intentions.
The variable itemQuantity was inferred as an Int because you gave it a whole number. The variable pricePerItem was inferred as a Double because of the decimal. In Swift, an Int is not a Double, and they cannot interact directly. The compiler isn't being difficult; it's preventing a whole category of precision bugs that plague other languages.
Explicit Casting for Type Safety
To fix this, you have to be explicit about the conversion. You need to tell Swift to treat the integer as a double for this specific calculation. Here is how we resolve it:
let itemQuantity = 3
let pricePerItem = 12.99
let totalCost = Double(itemQuantity) * pricePerItem
// Result: 38.97
By wrapping itemQuantity in Double(), you're creating a temporary double version of that number. Now both sides of the multiplication operator are the same type, and the compiler is happy.
How Swift Guesses Your Types
You might have noticed that I didn't write let itemQuantity: Int = 3. I just wrote let itemQuantity = 3. This is Type Inference. Swift is smart enough to look at the value on the right and assign the type automatically.
Here are the basic types you'll be using 90% of the time:
Int: Whole numbers (e.g.,42,-7).Double: Floating-point numbers for high precision (e.g.,3.14159).String: Text wrapped in double quotes (e.g.,"Swift is cool").Bool: Boolean values, eithertrueorfalse.
While inference is great for keeping code clean, there are times when you should provide a Type Annotation. For example, if you want a number to be a Double even though it's currently a whole number, you can do this:
let distance: Double = 10
// Without the ': Double', Swift would have made this an Int.
I usually rely on inference whenever possible to reduce noise, but if you're calling a function that strictly requires a Double, annotating your variable from the start saves you from having to cast it later.
📋 Practical Task
Building a Simple Trip Fuel Calculator
Your task is to create a small program that calculates the total cost of gas for a road trip. You'll need to handle different basic types and ensure they can interact without causing compiler errors.
Requirements:
- Create a variable for
distanceMilesas anInt(e.g., 450). - Create a variable for
milesPerGallonas aDouble(e.g., 25.5). - Create a variable for
pricePerGallonas aDouble(e.g., 3.89). - Calculate the
totalCost. You will need to divide the distance by the MPG and then multiply by the price. Remember: You cannot divide anIntby aDouble. - Print the final result to the console.
There are no comments for now.