Bird
Raised Fist0
Swiftprogramming~10 mins

Is operator for type checking in Swift - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Is operator for type checking
Start
↓
Have a variable
↓
Use 'is' to check type
↓
Evaluate if variable is of given type
↓
Return true
↓
Return false
↓
Use result in condition or print
↓
End
The 'is' operator checks if a variable is of a certain type and returns true or false.
Execution Sample
Swift
let value: Any = 42
if value is Int {
    print("It's an Int")
} else {
    print("Not an Int")
}
This code checks if 'value' is an Int and prints a message accordingly.
Execution Table
StepActionEvaluationResult
1Assign 42 to 'value' as Anyvalue = 42 (Any)value holds 42 as Any
2Check if 'value is Int'Is 42 an Int?true
3If condition true, printprint("It's an Int")Output: It's an Int
4End of if-elseNo else branch executedExecution ends
💡 'value is Int' is true, so the if branch runs and program ends.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
valueundefined42 (Any)42 (Any)42 (Any)
Key Moments - 2 Insights
Why does 'value is Int' return true even though 'value' is declared as Any?
Because 'value' holds the integer 42, and 'is' checks the actual stored type at runtime, not just the declared type. See execution_table step 2.
What happens if 'value' is not the checked type?
The 'is' operator returns false, so the else branch runs. This is shown by the logic flow in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of 'value is Int' at step 2?
Atrue
Bfalse
Cerror
Dunknown
💡 Hint
Check the 'Evaluation' and 'Result' columns in execution_table row 2.
At which step does the program print "It's an Int"?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table row 3.
If 'value' was assigned a String instead of 42, how would step 2's result change?
AIt would still be true
BIt would cause a compile error
CIt would be false
DIt would print an error message
💡 Hint
Refer to key_moments explanation about 'is' operator behavior with different types.
Concept Snapshot
Swift 'is' operator checks if a variable is of a certain type.
Syntax: variable is Type
Returns true if variable is that type, else false.
Used in conditions to run code based on type.
Works at runtime, checking actual stored type.
Full Transcript
This visual execution trace shows how the Swift 'is' operator works for type checking. We start by assigning the integer 42 to a variable 'value' declared as Any. Then, we use 'value is Int' to check if the stored value is an Int. The check returns true because the actual value is 42, an Int. Based on this, the program prints "It's an Int". The trace shows each step, variable states, and the output. Key moments clarify why the check works at runtime and what happens if the type does not match. The quiz tests understanding of these steps and outcomes.

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

  1. Step 1: Understand the purpose of is operator

    The is operator is used to check the type of a value in Swift.
  2. Step 2: Differentiate from other operators

    Unlike conversion or comparison, is only checks type and returns true or false.
  3. Final Answer:

    Checks if a value is of a certain type -> Option A
  4. 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

  1. Step 1: Recall Swift type checking syntax

    Swift uses is to check type, so item is String is correct.
  2. Step 2: Identify incorrect options

    == compares values, as is for casting, and instanceof is not Swift syntax.
  3. Final Answer:

    if item is String { } -> Option C
  4. 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

  1. Step 1: Analyze the variable type

    The variable value is declared as Any but holds the integer 42.
  2. Step 2: Check the is condition

    The condition value is Int checks if the value is an integer, which is true.
  3. Final Answer:

    Integer -> Option D
  4. 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

  1. Step 1: Understand casting syntax in Swift

    Direct casting with as requires the compiler to know the type; here obj is Any.
  2. 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.
  3. Final Answer:

    Incorrect casting syntax with as -> Option A
  4. 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

  1. Step 1: Understand the goal

    We want to print only elements that are strings from a mixed array.
  2. Step 2: Use correct type check syntax

    The is operator checks type correctly; item is String returns true for strings.
  3. Step 3: Identify incorrect options

    == String compares values, as String is invalid syntax here, and instanceof is not Swift.
  4. Final Answer:

    for item in items { if item is String { print(item) } } -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard