Bird
Raised Fist0
Swiftprogramming~10 mins

Type conversion is always explicit in Swift - Interactive Code Practice

Choose your learning style10 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.

Practice

(1/5)
1. In Swift, why must type conversion be explicit?
let x = 5 and let y = 3.2 are different types. What happens if you try to add x + y without conversion?
easy
A. Swift automatically converts x to a Double and adds them.
B. It causes a compile-time error because Swift does not convert types automatically.
C. The program runs but the result is always an integer.
D. Swift converts y to an Int silently.

Solution

  1. Step 1: Understand Swift's type safety

    Swift requires explicit type conversion to avoid unexpected behavior.
  2. Step 2: Check adding Int and Double without conversion

    Trying to add Int and Double directly causes a compile-time error.
  3. Final Answer:

    It causes a compile-time error because Swift does not convert types automatically. -> Option B
  4. Quick Check:

    Int + Double without conversion causes compile error [OK]
Hint: Remember: Swift never converts types automatically [OK]
Common Mistakes:
  • Assuming Swift converts Int to Double automatically
  • Thinking the result is always an Int
  • Believing silent conversion happens
2. Which of the following is the correct way to convert an Int num = 10 to a Double in Swift?
easy
A. let d = convert(num, to: Double)
B. let d = num as Double
C. let d = Double(num)
D. let d = num.toDouble()

Solution

  1. Step 1: Recall Swift's explicit conversion syntax

    Swift uses the target type name as a function to convert values.
  2. Step 2: Identify correct syntax for Int to Double

    The correct syntax is Double(num). Other options are invalid in Swift.
  3. Final Answer:

    let d = Double(num) -> Option C
  4. Quick Check:

    Target type as function: Double(num) [OK]
Hint: Use target type name with parentheses for conversion [OK]
Common Mistakes:
  • Using 'as' for numeric conversion
  • Calling non-existent methods like toDouble()
  • Using custom convert functions not in Swift
3. What is the output of this Swift code?
let a: Int = 7
let b: Double = 3.5
let c = Double(a) + b
print(c)
medium
A. 10.5
B. 10
C. 7.0
D. Error: Cannot add Int and Double

Solution

  1. Step 1: Convert Int to Double explicitly

    a is Int 7, converted to Double 7.0 using Double(a).
  2. Step 2: Add converted Double and b

    Adding 7.0 + 3.5 results in 10.5.
  3. Final Answer:

    10.5 -> Option A
  4. Quick Check:

    7.0 + 3.5 = 10.5 [OK]
Hint: Convert Int to Double before adding [OK]
Common Mistakes:
  • Forgetting to convert Int to Double
  • Expecting integer addition result
  • Assuming implicit conversion
4. Identify the error in this Swift code snippet:
let str = "123"
let num = Int(str)
let result = num + 10
print(result)
medium
A. Missing parentheses in Int conversion
B. String cannot be converted to Int
C. No error, code runs fine
D. Cannot add Int? and Int without unwrapping

Solution

  1. Step 1: Understand Int initializer returns optional

    Int(str) returns Int? because conversion might fail.
  2. Step 2: Adding optional Int and Int causes error

    You cannot add Int? and Int directly without unwrapping.
  3. Final Answer:

    Cannot add Int? and Int without unwrapping -> Option D
  4. Quick Check:

    Int(str) returns Int?, must unwrap before adding [OK]
Hint: Unwrap optionals before arithmetic operations [OK]
Common Mistakes:
  • Ignoring optional unwrapping
  • Assuming Int(str) returns Int directly
  • Trying to add optional and non-optional
5. You have an array of strings representing numbers: let arr = ["1", "2", "three", "4"]. You want to convert them to Ints and sum only the valid numbers. Which Swift code correctly does this?
hard
A. let sum = arr.compactMap { Int($0) }.reduce(0, +)
B. let sum = arr.map { Int($0) }.reduce(0, +)
C. let sum = arr.reduce(0) { $0 + Int($1)! }
D. let sum = arr.reduce(0) { $0 + Int($1) }

Solution

  1. Step 1: Use compactMap to convert and filter valid Ints

    compactMap converts strings to Ints and removes nil values safely.
  2. Step 2: Use reduce to sum the valid Ints

    reduce(0, +) sums all Ints in the array.
  3. Final Answer:

    let sum = arr.compactMap { Int($0) }.reduce(0, +) -> Option A
  4. Quick Check:

    compactMap filters nils, then sum [OK]
Hint: Use compactMap to safely convert and filter nils [OK]
Common Mistakes:
  • Using map which keeps optionals causing errors
  • Force unwrapping without checking validity
  • Adding optionals directly without unwrapping
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard