Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8529553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:03:27+00:00 2026-06-11T09:03:27+00:00

Possible Duplicate: Why does fast enumeration not skip the NSNumbers when I specify NSStrings?

  • 0

Possible Duplicate:
Why does fast enumeration not skip the NSNumbers when I specify NSStrings?

I noticed some unexpected behavior while using fast enumeration recently. In hindsight I was probably expecting fast enumeration to do more than it’s intended for, so I’m looking for some clarification on how it’s actually works.

Say I have a parent class Shape with 2 child classes, 3SidedShape and 4SidedShape. I have an array called myShapes, that contains objects from both the 3 and 4 sided classes.

If I wanted to search through the array myShapes, but I’m only concerned with 3 sides shapes what I was doing is:

 for (3SidedShape *shape in myShapes)

My thought was that I would only be iterating over objects of class 3SidedShape, but that is not the case? I guess I’m casting all objects as 3SidedShape whether they like it or not. I’m evening returning the object after as a completely different class. Granted I’m not calling any methods that both classes don’t have, but I didn’t expect class siblings to just re-cast so easily without a hitch? Did I just get lucky here or can you really enumerate as any class you please regardless of relation? Can anyone explain what actually happens during enumeration?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T09:03:28+00:00Added an answer on June 11, 2026 at 9:03 am

    The type specified in a for…in loop, aka fast enumeration, casts all the elements in the collection to the specified type. The reason why they are “re-cast so easily” is that casting does NOT turn one type of object into another (how would that work?). It’s a hint to the compiler telling it to treat the object as if it were the other type, as if to say “don’t worry, this object is of (insert type), so type check it as such.” Sending the object a message it can’t handle, but the type it was casted to can, will still crash the app. What you should do is this:

    for (id shape in myShapes){
        if ([shape isKindOfClass: [3SidedShape class]]){
            //insert code here
        }
    }
    

    That code assumes nothing of type, using introspection to only perform the code for objects who are of type 3SidedShape or a subclass of 3SidedShape. For exact checking (excluding subclasses) use isMemberOfClass:. Be wary of using isMemberOfClass: to test membership of a class in a class cluster (NSNumber), however, due to their more complex internal implementation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Does fast enumeration in Objective-C guarantee the order of iteration? Just a
Possible Duplicate: What does “(void) new” mean in C++? I'm not familiar with C++
Possible Duplicate: Does anyone beside me just NOT get ASP.NET MVC? I dont know
Possible Duplicate: Does not contain a constructor that takes 0 arguments I have a
Possible Duplicate: Why does C# not provide the C++ style ‘friend’ keyword? I'd like
Possible Duplicate: does c++ standard prohibit the void main() prototype? Why is C++ not
Possible Duplicate: Does C# support return type covariance? I'm not sure if I'm just
Possible Duplicate: Does the compiler continue evaluating an expression where all must be true
Possible Duplicate: Does anyone know of a good C# API for Subversion? I'm designing
Possible Duplicate: How does this CSS triangle shape work? Please help me i need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.