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 Posted / 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 |
Post New Answer View All Answers
Why does apple use swift?
What is lazy loading in ios swift?
What are generics? How to make a method or variable generics in swift?
What is the full meaning of swift?
Is swift object oriented?
How can we make a property optional in swift?
What is indexpath in swift?
How to pass the data between view controllers?
Should I use struct or class swift?
Do loops swift?
What are type methods in swift?
How do you make a swift bridging header?
Is swift similar to c?
How to convert nsmutablearray to swift array in swift?
Explain the difference between let and var in swift programming?