Consider the following code:
let op1: Int = 1
let op2: UInt = 2
let op3: Double = 3.34
var result = op1 + op2 + op3
Where is the error and why? How can it be fixed?
Answer / iosraj
Swift doesn’t define any implicit cast between data types, even if they are conceptually almost identical (like UInt and Int).
To fix the error, rather than casting, an explicit conversion is required. In the sample code, all expression operands must be converted to a common same type, which in this case is Double:
var result = Double(op1) + Double(op2) + op3
| Is This Answer Correct ? | 4 Yes | 0 No |
What are ui elements?
Explain the common execution states for a swift ios app (ios application lifecycle).
Can you explain completion handler?
Mention what are the features of Swift Programming?
What is the use of continue statement in swift loop?
Do swift classes inherit from nsobject?
What is clean swift?
What language is similar to swift?
What language is swift most similar to?
What is a swift protocol?
Explain completion handler?
What is a delegate in swift?