Swift Type Inference


Swift Type Inference (Let Swift Guess the Type)

Swift is smart — it can figure out the type of a value without you needing to say it.

Instead of writing the type every time, Swift checks the value and guesses what type it should be.

Example:

let city = "New York"     // Swift sees it's text → So, it's a String 
let score = 95            // Swift sees it's a number → So, it's an Int 
let pi = 3.14             // Swift sees a decimal → So, it's a Double 
Let isSunny = true        // Swift sees true/false → So, it's a Bool 

You didn’t write String, Int, Double, or Bool — Swift understood automatically.


Why It’s Helpful:

  • Makes code cleaner
  • Reduces extra typing
  • Still keeps your code safe and strongly typed

When to Use It:

Use type inference when the value is clear, and Swift can easily detect the type.

If the value is not obvious or ambiguous, you might still need to explicitly write the type.


Prefer Learning by Watching?

Watch these YouTube tutorials to understand GIT Tutorial visually:

What You'll Learn:
  • 📌 Type Inference - Your First iOS and SwiftUI App with Xcode 11, Swift 5.1 and iOS 13
  • 📌 05-Type Annotation | Type Inference | Type Safety | iOS | Swift Programming Language |iPhone|iPad
Previous Next