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


Please Help Members By Posting Answers For Below Questions

What is tuple? How to create a tuple in swift?

496


Should I use struct or class swift?

456


What is nsdictionary in swift?

439


What are the characteristics of switch?

484


What is mvc architecture in swift?

491






What are the best ways of achieving concurrency in ios?

486


How can we make a property optional in swift?

492


What is swift stand for?

453


Is swift difficult to learn?

465


How proficient are you in objective-c and swift? Can you briefly describe their differences?

675


What is mutating keyword in swift?

522


What is nil in swift?

444


Explain enum in swift.

610


How can you write a multiple line comment swift?

470


Which is better car baleno or swift?

449