0
0
Swiftprogramming~10 mins

How Swift compiles to native code - Interactive Practice

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

Complete the code to declare a simple Swift function.

Swift
func greet() -> String {
    return [1]
}
Drag options to blanks, or click blank then click option'
Areturn Hello
BHello, world!
C"Hello, world!"
Dprint("Hello, world!")
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Trying to return a print statement.
2fill in blank
medium

Complete the code to declare a variable with type Int.

Swift
var number: [1] = 10
Drag options to blanks, or click blank then click option'
AInt
BDouble
CString
DBool
Attempts:
3 left
💡 Hint
Common Mistakes
Using String or Double instead of Int.
Omitting the type annotation.
3fill in blank
hard

Fix the error in the function declaration to compile correctly.

Swift
func add(a: Int, b: Int) -> [1] {
    return a + b
}
Drag options to blanks, or click blank then click option'
AInt
BString
CBool
DVoid
Attempts:
3 left
💡 Hint
Common Mistakes
Using String or Bool as return type incorrectly.
Using Void when the function returns a value.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps numbers to their squares for numbers greater than 3.

Swift
let squares = Dictionary(uniqueKeysWithValues: (4...7).map { [1] in let [2] = [1] * [1]; ([1], [2]) })
Drag options to blanks, or click blank then click option'
Anum
Bval
Cx
Dy
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not matching the closure parameters with the map function.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase letters to their ASCII values for letters 'a' to 'd'.

Swift
let asciiDict = Dictionary(uniqueKeysWithValues: ("a"..."d").map { [1] in let [2] = [1].uppercased(); ([1].uppercased(), Int([3].unicodeScalars.first!.value)) })
Drag options to blanks, or click blank then click option'
Aletter
Bchar
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names.
Not converting the character to uppercase for the key.