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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:30:54+00:00 2026-05-27T11:30:54+00:00

I have created a class named Foo : @interface Foo:NSObject{ int myInt; } @property

  • 0

I have created a class named Foo:

@interface Foo:NSObject{
     int myInt;
}
@property int myInt;
@end

and a subclass of Foo named Bar:

@interface Bar:Foo{
     NSString *myString;
}
@property (copy) NSString *myString;
@end

I am trying to store Bar as a Foo object in an array, like this:

-(void)createBar{
     Foo *object = [[Bar alloc]init];

     // myArray is an instance of NSMutableArray
     [myArray addObject:object];
}

I am doing this because I actually have more than one subclass of Foo (I don’t want to list them all). When I grab an object from the array and send the message to the object to get the myString variable, the application doesn’t do anything. Example:

-(NSString *)getStringFromFooAtIndex(NSUInteger)index{
     Foo *object = [myArray objectAtIndex:index];

     return [object myString];
}

Am I misunderstanding how the ‘message’ works? I was under the assumption that I can send a message to an object and it would call it whether it was there or not. Do I need to be doing this some other way? The array will hold all the different types of Foo child classes and I need it to store them there.

  • 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-05-27T11:30:55+00:00Added an answer on May 27, 2026 at 11:30 am

    I was under the assumption that I can send a message to an object and it would call it whether it was there or not.

    You can indeed send any message to any object; that’s part of the fun of Objective-C. The type of the variable (Foo *, Bar *, id, or anything else) has no effect on the message send. The object to which the variable points knows its class. The lookup of the corresponding method is done at runtime, via that class. The compiler turns the bracketed expression into a call to a function, objc_msgSend.

    You should be getting a warning about [object myString] when building, saying “‘Foo’ may not respond to ‘myString'” — the compiler knows that there’s at least one class somewhere that has a method corresponding to myString, and it knows that, at compile-time, Foo doesn’t seem to be one of those, but it can’t guarantee that the Foo won’t be able to do something with the message at runtime. Messages can be resolved in custom ways during runtime. Notice that if you change the type of the variable to id, the warning disappears — the compiler can no longer reason about what methods are available.

    If it turns out that the object to which you send myString doesn’t respond (i.e., if the object really is a Foo instead of a Bar, or one of Foo‘s subclasses that doesn’t implement myString), an exception will be raised. This is the default response (by anything that inherits from NSObject) to an unrecognized message.

    If you have a heterogenous collection of objects to which you need to send messages, you will probably want to test each object first. You can do as jstevenco suggested, and test for functionality:

    if( [object respondsToSelector:@selector(myString)] ){
    

    or test for identity:

    if( [object isKindOfClass:[Bar class]] ){
    

    The latter will pass if the object is a Bar or any of Bar‘s subclasses. Use isMemberOfClass: to test only for the class you specify.

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

Sidebar

Related Questions

I have created a class named Times and I have to construct 4 overloaded
If I have an abstract class in java named Foo and it has an
Say I have a structure like: class SomeObject Public Name as String Public Created
I have created a class for a dashboard item which will hold information such
I have created UITableCellView class called NoteCell . The header defines the following: #import
I have created a class library in VB .NET. Some code in the library
I have created a class library called AddServiceLibrary in which I have a method
I am using C#. I have created a class which can be included in
Need some help about with Memcache. I have created a class and want to
I am using the apache library. I have created a class which sends a

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.