0
0
Swiftprogramming~5 mins

Type conversion is always explicit in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean that type conversion is always explicit in Swift?
It means you must clearly tell Swift when you want to convert one type to another. Swift won't do it automatically for you.
Click to reveal answer
beginner
How do you convert an Int to a Double in Swift?
You write Double(yourIntValue). For example, Double(5) converts the integer 5 to a double 5.0.
Click to reveal answer
intermediate
Why does Swift require explicit type conversion?
To avoid mistakes and bugs by making sure you know exactly when and how data changes type.
Click to reveal answer
beginner
What happens if you try to add an Int and a Double without conversion in Swift?
Swift will give an error because it does not automatically convert types. You must convert one type explicitly before adding.
Click to reveal answer
intermediate
Show an example of explicit type conversion from String to Int in Swift.
You use Int("123") which tries to convert the string "123" to the integer 123. It returns an optional Int because the conversion might fail.
Click to reveal answer
In Swift, how do you convert an Int value 10 to a Double?
ADouble(10)
B10 as Double
Cconvert(10, to: Double)
D10.toDouble()
What happens if you try to add an Int and a Double directly in Swift without conversion?
AYou get a compile-time error.
BSwift automatically converts Int to Double.
CIt works fine.
DThe result is always an Int.
Which of these is a correct way to convert a String "42" to an Int in Swift?
A"42" as Int
BInt("42")
Cconvert("42", to: Int)
D"42" toInt()
Why does Swift require explicit type conversion?
ATo allow automatic type guessing.
BTo make code run faster.
CBecause Swift is an old language.
DTo avoid unexpected bugs by making conversions clear.
Which statement is true about type conversion in Swift?
ASwift automatically converts types when needed.
BType conversion is implicit for numeric types only.
CType conversion must always be explicit.
DYou cannot convert types in Swift.
Explain why Swift requires explicit type conversion and give an example converting Int to Double.
Think about safety and clarity in code.
You got /2 concepts.
    Describe how to convert a String to an Int in Swift and what happens if the string cannot be converted.
    Remember the conversion returns an optional because it might fail.
    You got /3 concepts.