Hi. I am new to swift and am having some trouble understanding
From what I understand
Optional types are used when something may not have a designated value. However, many operations require the value to be non-optional which requires unwrapping.
Most sources I have come across say we need
! after the variable name to unwrap it. However, if the value is indeed
nil then it would throw an error. This is why it is discouraged to use this approach (to the best of my understanding).
The other way is to do optional unwrapping. However, I am unable to get this part correctly, because at the end I am still having to do the unwrapping via
As an example suppose I have the following:
let nums : [Int] = [1, 2, 3, 5] let nums_dct : [Int : Int] = [0:1, 1: 2, 2: 3, 3: 5] nums[nums_dct] // throws an error nums[nums_dct!] //doesn't throw an error
The error is due to
Int? output. What is an elegant way to do the above without using