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 8461583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:55:18+00:00 2026-06-10T13:55:18+00:00

Given the following code: for (id object in anArray){ if ([object isKindOfClass:[ClassOne class]]){ ClassOne

  • 0

Given the following code:

for (id object in anArray){
    if ([object isKindOfClass:[ClassOne class]]){
       ClassOne *myObj = [[ClassOne alloc] init];
    }else if ([object isKindOfClass:[ClassTwo class]]){
       ClassTwo *myObj = [[ClassTwo alloc] init];
    }
    myObj.property = TRUE;
}

the compiler will raise an error regarding myObj (undeclared identifier), which is somehow obvious (“what should I do if both conditions will be false?”). That means I have to define the object before the if-else block, but which type of object I have to use? If I use id there will be errors on myObj.property = TRUE;, if I use ClassOne or ClassTwo there will be some warnings regarding incompatible pointer assignment. Should I use some other way instead of the given code?

Thank you.

(note: the snippet was written without using syntax checking or testing it, so it may contains errors)

  • 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-10T13:55:20+00:00Added an answer on June 10, 2026 at 1:55 pm

    How about a protocol?

    @protocol MyProtocol
    
    @property (assign, nonatomic) BOOL myProperty;
    
    @end
    

    Then:

    for (id anObject in anArray){
        if ([anObject conformsToProtocol:@protocol(MyProtocol)]) {
            Class clazz = [anObject class];
            id<MyProtocol>  myObject = [[clazz alloc] init];
            myObject.myProperty = YES;
        }
    }
    

    This is the cleanest and shortest solution I can think of. You won’t need to cast, instead you will harness the power of objective-c’s dynamic binding. You won’t have to add too many if-else statements. Instead you just make sure the object that comes in, conforms to your protocol.

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

Sidebar

Related Questions

Given the following code: case class ChangeSet(field:String, from:Object, to:Object) private var changed:List[ChangeSet] = Nil
Given the following code; public class CustomControl { private object _dataItem; public object DataItem
Given the following code is it posible to access an object's property from inside
Given the following code: final class retVal { int photo_id; } Gson gson =
Given the following code, is there a way I can call class A's version
Given the following code : public class Game { private Map<String,Coordinate> m_myHashMap; ... //
Given the following code... class Program { static void Main(string[] args) { Foo foo
Given the following code: class C { static void m(String s) { ... }
Given the following code, how can I iterate over an object of type ProfileCollection?
Given the following code: [example.h] class MyClass { public: MyClass(); std::string name; std::string address;

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.