Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as' instead of 'is' for type checking.
✗ Incorrect
The is operator checks if a variable is of a certain type in Swift.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as' instead of 'is' for checking type.
✗ Incorrect
Use is to check type before force casting with as!.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as' instead of 'is' causes a compile error.
✗ Incorrect
The is operator is used for type checking in Swift, not as.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as!' for casting without checking type first.
✗ Incorrect
Use is to check type and as? for safe casting.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'as!' without checking type, or using wrong operator for addition.
✗ Incorrect
Check type with is, safely cast with as?, then add 10 with +.