0
0
Swiftprogramming~10 mins

Is operator for type checking 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 check if variable 'item' is of type String.

Swift
if item [1] String {
    print("It's a string")
}
Drag options to blanks, or click blank then click option'
A==
Bas
Cis
Dinstanceof
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as' instead of 'is' for type checking.
2fill in blank
medium

Complete the code to check if 'value' is an Int before casting.

Swift
if value [1] Int {
    let number = value as! Int
    print(number)
}
Drag options to blanks, or click blank then click option'
Ais
Bas
C==
Dinstanceof
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as' instead of 'is' for checking type.
3fill in blank
hard

Fix the error in the code to check if 'obj' is a Double.

Swift
if obj [1] Double {
    print("Double value")
}
Drag options to blanks, or click blank then click option'
Ais
Binstanceof
C==
Das
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as' instead of 'is' causes a compile error.
4fill in blank
hard

Fill both blanks to check if 'element' is a String and then cast it safely.

Swift
if element [1] String {
    if let str = element [2] String {
        print(str)
    }
}
Drag options to blanks, or click blank then click option'
Ais
Bas?
Cas!
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as!' for casting without checking type first.
5fill in blank
hard

Fill all three blanks to check if 'data' is an Int, cast it safely, and print it.

Swift
if data [1] Int {
    if let number = data [2] Int {
        print(number [3] 10)
    }
}
Drag options to blanks, or click blank then click option'
Ais
Bas?
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as!' without checking type, or using wrong operator for addition.