When it comes to coding, less is more. To make the programs readable and efficient, programmers frequently try to keep their code as short as possible. So, we use type Inference for this purpose in Swift language.
The compiler can automatically determine the data type of a Swift expression using type inference. We can declare dictionaries with less code by using type inference.
Our code looked like this when we first learned how to declare a dictionary:
var Shop: [String: Int] = [ "Juices": 21, "Snacks": 14 ];
We can use type inference to initialize
Shop with the following syntax:
var Shop = [ "Juices": 21, "Snacks": 14 ];
The type annotation
[String: Int] was removed from the initialization.
The compiler deduces that the keys are of type
String and the values are of type
Int using type inference.
Swift’s Language Guide has more information on type inference.
Here are some useful tools to help you along your journey!
Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.