The is operator checks if an instance is of a certain type or inherits from that type. It returns true if the instance matches the type, otherwise false.
Click to reveal answer
beginner
How do you use the is operator in an if statement?
You write: if object is SomeType { /* code */ }. This runs the code inside the block only if object is of type SomeType or a subclass.
Click to reveal answer
intermediate
Can the is operator check for protocol conformance?
Yes! The <code>is</code> operator can check if an instance conforms to a protocol, not just class or struct types.
Click to reveal answer
intermediate
What is the difference between is and as? in Swift?
is checks if an instance is of a type and returns a Boolean. as? tries to cast the instance to that type and returns an optional value.
Click to reveal answer
beginner
Example: What does this code print?<br><pre>let value: Any = 5<br>if value is Int { print("It's an Int") }</pre>
It prints: It's an Int because the value 5 is an integer and the is operator confirms that.
Click to reveal answer
What does the is operator return when used in Swift?
AThe memory address of the instance
BThe instance cast to the specified type
CThe size of the instance in bytes
DA Boolean indicating if the instance matches the type
✗ Incorrect
The is operator returns true or false depending on whether the instance is of the specified type.
Which of these can the is operator check for?
AProtocol conformance
BClass type only
CVariable value
DFunction return type
✗ Incorrect
The is operator can check if an instance conforms to a protocol.
What will this code print?
let text: Any = "Hello" if text is String { print("It's a String") }
AError
BNothing
CIt's a String
DIt's an Int
✗ Incorrect
The variable text holds a string, so text is String is true.
What is the result of value is Double if value is an Int?
Atrue
Bfalse
CRuntime error
DOptional Double
✗ Incorrect
An Int is not a Double, so the check returns false.
Which operator tries to convert an instance to a type and returns an optional?
Aas?
Bis
C==
D!=
✗ Incorrect
as? attempts to cast and returns an optional value.
Explain how the is operator works in Swift and give a simple example.
Think about how you check if something is a certain kind of thing in real life.
You got /3 concepts.
Describe the difference between is and as? operators in Swift.
One checks type, the other tries to change type.
You got /3 concepts.
Practice
(1/5)
1. What does the is operator do in Swift?
easy
A. Checks if a value is of a certain type
B. Converts a value to another type
C. Declares a new variable
D. Compares two values for equality
Solution
Step 1: Understand the purpose of is operator
The is operator is used to check the type of a value in Swift.
Step 2: Differentiate from other operators
Unlike conversion or comparison, is only checks type and returns true or false.
Final Answer:
Checks if a value is of a certain type -> Option A
Quick Check:
is operator = type check [OK]
Hint: Remember: is checks type, not value or conversion [OK]
Common Mistakes:
Confusing is with type casting
Thinking is compares values
Using is to convert types
2. Which of the following is the correct syntax to check if variable item is of type String in Swift?
easy
A. if item == String { }
B. if item as String { }
C. if item is String { }
D. if item instanceof String { }
Solution
Step 1: Recall Swift type checking syntax
Swift uses is to check type, so item is String is correct.
Step 2: Identify incorrect options
== compares values, as is for casting, and instanceof is not Swift syntax.
Final Answer:
if item is String { } -> Option C
Quick Check:
Type check syntax = is [OK]
Hint: Use is for type checks, not as or == [OK]
Common Mistakes:
Using == instead of is
Confusing is with as
Using JavaScript's instanceof in Swift
3. What will be the output of the following Swift code?
let value: Any = 42
if value is Int {
print("Integer")
} else {
print("Not an Integer")
}
medium
A. Runtime error
B. Not an Integer
C. Compilation error
D. Integer
Solution
Step 1: Analyze the variable type
The variable value is declared as Any but holds the integer 42.
Step 2: Check the is condition
The condition value is Int checks if the value is an integer, which is true.
Final Answer:
Integer -> Option D
Quick Check:
is Int on 42 = true [OK]
Hint: Check actual stored type, not declared type [OK]
Common Mistakes:
Assuming Any means not Int
Expecting a compile error for is
Confusing is with casting
4. Identify the error in the following Swift code snippet:
let obj: Any = "Hello"
if obj is String {
print(obj as String)
}
medium
A. Incorrect casting syntax with as
B. Using is instead of as?
C. Missing optional binding for safe cast
D. No error, code runs correctly
Solution
Step 1: Understand casting syntax in Swift
Direct casting with as requires the compiler to know the type; here obj is Any.
Step 2: Identify the correct cast usage
Safe cast should use as? with optional binding or forced cast as!. Using as String alone causes error.
Final Answer:
Incorrect casting syntax with as -> Option A
Quick Check:
Use as? or as! for casting [OK]
Hint: Use as? for safe cast, not plain as [OK]
Common Mistakes:
Using as without optional binding
Ignoring safe cast with as?
Assuming is casts automatically
5. Given a list of mixed types: let items: [Any] = [1, "two", 3.0, "four"], which Swift code correctly prints only the strings using the is operator?
hard
A. for item in items { if item == String { print(item) } }
B. for item in items { if item is String { print(item) } }
C. for item in items { if item as String { print(item) } }
D. for item in items { if item instanceof String { print(item) } }
Solution
Step 1: Understand the goal
We want to print only elements that are strings from a mixed array.
Step 2: Use correct type check syntax
The is operator checks type correctly; item is String returns true for strings.
Step 3: Identify incorrect options
== String compares values, as String is invalid syntax here, and instanceof is not Swift.
Final Answer:
for item in items { if item is String { print(item) } } -> Option B
Quick Check:
Use is to filter by type [OK]
Hint: Loop and check type with is to filter items [OK]
Common Mistakes:
Using equality instead of type check
Trying to cast inside condition incorrectly
Using non-Swift keywords like instanceof
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.