Optionals in Swift - not assigning default value for optionals [duplicate] - swift

This question already has answers here:
Why optional constant does not automatically have a default value of nil [duplicate]
(2 answers)
Constant unassigned optional will not be nil by default
(1 answer)
Closed 5 years ago.
let company : String? // = nil
if company == nil {
var newCompany = company ?? "apple"
}
In this code identifier company we need to assign as
let company : String? = nil
but apple documentation is telling that no need for assigning nil value

You declared a constant without a value and without an initializer that can change it, which is nonsensical. The compiler, therefore, warns you that you should initialize the constant.
Change let to var and your code will work fine.

Related

Constant used before being initialzed [duplicate]

This question already has an answer here:
Constant unassigned optional will not be nil by default
(1 answer)
Closed 4 years ago.
let errorCodestring : String?
if let theError = errorCodestring {
print(theError)
}
In the above code Xcode throws the error:
constant 'errorCodestring'being used before being initialized
What is wrong in the code and how to clear the error?
You have to initialize the constant before using it.
let errorCodestring : String? = nil

multiple optional binding for a immutable variable and an immutable [duplicate]

This question already has an answer here:
Constant unassigned optional will not be nil by default
(1 answer)
Closed 6 years ago.
The following code works fine
struct carConfi {
var owner: String?
let brand: String = "BMW"
var currentMile: Double = 2000
}
let tomCar = carConfi()
However, if I change the type of the property owner to constant, there will be an error at the initializer
struct carConfi {
let owner: String? // Change to constant
let brand: String = "BMW"
var currentMile: Double = 2000
}
let tomCar = carConfi() //error: missing argument for parameter 'owner' in call
I did a bit search, it turns out that it is because the optional variables automatically have a default value of nil
I guess: Because once the constant is set, it then cannot be changed, if the optional constant automatically received an nil then it will keep as an unchangeable nil that's very silly and may against the users will
Question: My college doesn't fully convinced by the guess, he told me there must be more reasons for that. I would very appreciate if someone can explain that to me
Thx
Not setting a read-only (constant) field with either an:
initialization expression
initializer
is almost certainly an indication of an error in your program.
Since you have no other opportunity to set the value of your let field, the value of the field is going to remain nil (or some other default). It is rather unlikely that a programmer would find such behavior desirable, and request it on purpose.
That is why Swift marks this situation as an error. On the other hand, if you actually wanted your String constant to remain nil, you could add an expression to set it to nil, and silence the error:
let owner: String? = nil // Pretty useless, but allowed
Constants are set once, and once only. If you wanted it to be null or 0, then you would set it. You always define a constant on initiation.

Why optional constant does not automatically have a default value of nil [duplicate]

This question already has an answer here:
Constant unassigned optional will not be nil by default
(1 answer)
Closed 6 years ago.
The following code works fine
struct carConfi {
var owner: String?
let brand: String = "BMW"
var currentMile: Double = 2000
}
let tomCar = carConfi()
However, if I change the type of the property owner to constant, there will be an error at the initializer
struct carConfi {
let owner: String? // Change to constant
let brand: String = "BMW"
var currentMile: Double = 2000
}
let tomCar = carConfi() //error: missing argument for parameter 'owner' in call
I did a bit search, it turns out that it is because the optional variables automatically have a default value of nil
I guess: Because once the constant is set, it then cannot be changed, if the optional constant automatically received an nil then it will keep as an unchangeable nil that's very silly and may against the users will
Question: My college doesn't fully convinced by the guess, he told me there must be more reasons for that. I would very appreciate if someone can explain that to me
Thx
Not setting a read-only (constant) field with either an:
initialization expression
initializer
is almost certainly an indication of an error in your program.
Since you have no other opportunity to set the value of your let field, the value of the field is going to remain nil (or some other default). It is rather unlikely that a programmer would find such behavior desirable, and request it on purpose.
That is why Swift marks this situation as an error. On the other hand, if you actually wanted your String constant to remain nil, you could add an expression to set it to nil, and silence the error:
let owner: String? = nil // Pretty useless, but allowed
Constants are set once, and once only. If you wanted it to be null or 0, then you would set it. You always define a constant on initiation.

Swift shorthand to access property versus default value [duplicate]

This question already has answers here:
Providing a default value for an Optional in Swift?
(5 answers)
Closed 7 years ago.
Does Swift have a shorthand syntax for substituting default values when trying to access properties on an optional? For example:
let value = anOptional != nil ? anOptional.value : defaultValue
This is not a question about the simple use of the ?? coalescing operator, but a question about a shorthand syntax for assigning from a property on a non-nil optional.
You can do this let value = anOptional ?? defaultValue

Why can I use a constant initialization as if-condition? [duplicate]

This question already has answers here:
How is Swift `if let` evaluated?
(5 answers)
Closed 7 years ago.
While reading the official Apple Guide I found this
var optionalName: String? = "John Appleseed"
var greeting = "Hello!"
if let name = optionalName {
greeting = "Hello \(name)"
}
There is a constant declaration and assignment where I — as a beginner — expected an expression returning a boolean. But the condition of this if statement seems to get true as the value, because the code inside the parentheses is being executed.
Does the initialization of a constant return a boolean value or something that a if statement can use as a condition?
This is called Optional Binding. Quoting from the Basics -> Optionals -> Optional Binding section of the Swift Language Guide.
You use optional binding to find out whether an optional contains a
value, and if so, to make that value available as a temporary constant
or variable. Optional binding can be used with if and while statements
to check for a value inside an optional, and to extract that value
into a constant or variable, as part of a single action.
What this if let construct is doing is checking if someOptional is exists. If it isn't nil/.None then it "binds" the Optional value to a constant.
if let constantName = someOptional {
...
}
This can be thought of as:
if someOptional != nil {
let constantName = someOptional!
....
}