WTF? How can this work?
WTF? How can this work?
- Subject: WTF? How can this work?
- From: Mike Beam <email@hidden>
- Date: Sat, 18 Aug 2001 22:30:19 -0500
Maybe I'm just not thinking straight at all right now, but how can the
following work? I have a straighforward application who's sole purpose is
to test this little confusion of mine. It's interface is a text field,
wired up to an action method of a class, Controller, called add: Here is
the code for Controller.h:
#import <Cocoa/Cocoa.h>
@interface Controller : NSObject
{
NSArray *array;
}
- (IBAction)add:(id)sender;
@end
And Controller.m:
#import "Controller.h"
@implementation Controller
- (void)awakeFromNib
{
array = [[NSArray alloc] init];
}
- (IBAction)add:(id)sender
{
[array addObject:[sender stringValue]];
NSLog([array description]);
}
@end
Now of course when I compile this I get the warning that NSArray doesn't
respond to addObject:, but when I run the app, enter some text into the
textfield and hit enter, thereby invoking add:, in the console appears my
text as an array description. Repeating this adds newly typed text into the
array and displays in the PB console the description of the array, with the
newly added elements.
Again, maybe I'm *completely* missing something fundamental here, but I
though NSArrays were _immutable_. It seems like this one here is behaving
exactly like an NSMutableArray. Is this behavior correct? Did the compiler
notice me trying to send addObject to an NSArray and decide that I really
wanted array to be typed NSMutableArray? Any help would be appreciated.
Thanks!
Mike Beam