0
0
Swiftprogramming~10 mins

Type conversion is always explicit in Swift - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to convert an Int to a String explicitly.

Swift
let number = 42
let text = String([1])
Drag options to blanks, or click blank then click option'
Anumber
BInt(number)
C"number"
DtoString(number)
Attempts:
3 left
💡 Hint
Common Mistakes
Using quotes around the variable name, which makes it a string literal.
Trying to use a function like toString which doesn't exist in Swift.
Using Int(number) which is redundant and incorrect here.
2fill in blank
medium

Complete the code to convert a String to an Int explicitly.

Swift
let text = "123"
let number = Int([1])
Drag options to blanks, or click blank then click option'
A123
Btext
C"text"
DString(text)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number literal instead of the variable.
Putting quotes around the variable name, making it a string literal.
Trying to convert a String to String again.
3fill in blank
hard

Fix the error in the code by completing the explicit conversion.

Swift
let pi = 3.14
let integerPi = [1](pi)
Drag options to blanks, or click blank then click option'
AFloat
BString
CDouble
DInt
Attempts:
3 left
💡 Hint
Common Mistakes
Using String() which converts to text, not number.
Using Double() which does not change the type.
Using Float() which is a different floating-point type.
4fill in blank
hard

Fill both blanks to convert a String to a Double explicitly and then to an Int.

Swift
let text = "56.78"
if let doubleValue = [1](text) {
    let intValue = [2](doubleValue)
}
Drag options to blanks, or click blank then click option'
ADouble
BInt
CString
DFloat
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to convert String directly to Int without Double.
Using String() instead of Double() for the first conversion.
Using Float() instead of Double() for the first conversion.
5fill in blank
hard

Fill all three blanks to convert an Int to String, then to Double, and finally to Int again.

Swift
let number = 100
let text = [1](number)
if let doubleValue = [2](text) {
    let finalInt = [3](doubleValue)
}
Drag options to blanks, or click blank then click option'
AString
BDouble
CInt
DFloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using Float instead of Double for conversion.
Skipping the optional binding for Double conversion.
Trying to convert String directly to Int without Double.