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
What do you mean by the term “defer”?
What is retain in swift?
What is $0 in swift?
What is difference between xcode and swift?
What is a static variable swift?
What is a swift protocol?
Do loops swift?
Is swift similar to python?
How can you make a property optional in swift?
How do I create a swift file in xcode?
How you define variables in swift?
What is difference between single and double, in swift?
Can you explain any three-shift pattern matching techniques?
What is the significance of “?” In swift?
Is swift object oriented programming?