Why do I need to specify what type a variable is in a class in Swift?
ios, swift
Solution
The types are only inferred if you assign a default value initially. From an example in the Language Reference, either declare the type:
var welcomeMessage: String
or assign an initial value that allows Swift to infer the type:
welcomeMessage = "Hello"
In the welcomeMessage example above, no initial value is provided, and so the type of the welcomeMessage variable is specified with a type annotation rather than being inferred from an initial value.
Problem
I'm just wondering. As I understand, `var` and `let` can be anything and `Swift` automates the right type like in `JavaScript`. But when I try to set properties in a class I get an error when I don't specify the type. ``` var value1, value2 // Error: missing annotations ``` Well, I've read some references and the variable requires a type on declaration like `var foo = 0`. But in my class I have an `init()` which will set the variables to whatever I input when creating the object of the class. So how should I achieve this? Is it even possible? I saw the type `typealias` but that didn't work either.