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.