[OT] [RANT]NSEnumerator
[OT] [RANT]NSEnumerator
- Subject: [OT] [RANT]NSEnumerator
- From: Isaac Sherman <email@hidden>
- Date: Fri, 01 Feb 2002 00:04:37 -0500
This is odd. I've been having a problem with NSEnumerators. They always
seem to return nil, when I go to use them in a while loop, and the loop
never starts. So, I wrote up a quick test, and it seems that you have to
send an NSEnumerator ivar (at least one created from an NSMutable
Dictionary) 2 nextObject messages instead of one (at least with custom
classes.
If this is annoying, or discussed in the docs, please show me where. I
checked the docs on NSEnumerator once I got my results, but it never states
that you need 2. It implies 1, even in the sample code.
Here's the code:
#import "StringThing.h"
#import <Cocoa/Cocoa.h>
void test(void);
int main(int argc, const char *argv[])
{
test();
return 0;
}
void test(void)
{
NSMutableDictionary * testDict=[[[NSMutableDictionary alloc]init]retain];
StringThing * stringThing1=[[[StringThing alloc]initWithStrings:@"Foo"
string2:@"bar"]retain];
StringThing * stringThing2=[[[StringThing alloc]initWithStrings:@"Snafu"
string2:@"Dyah!"]retain];
StringThing * stringThing3=[[[StringThing alloc]init]retain];
NSEnumerator * e=[[NSEnumerator alloc]init];
[testDict setObject:stringThing1 forKey:@"stringThing1"];
[testDict setObject:stringThing2 forKey:@"stringThing2"];
[testDict setObject:stringThing3 forKey:@"stringThing3"];
e=[testDict objectEnumerator];
NSLog(@"e has just been made. This is the normal output:\n");
NSLog(@"stringThing1 getString1=%@\tstringThing1 getString2=%@\n",
[stringThing1 getString1], [stringThing1 getString2]);
NSLog(@"stringThing2 getString1=%@\tstringThing2 getString2=%@\n",
[stringThing2 getString1], [stringThing2 getString2]);
NSLog(@"stringThing3 getString1=%@\tstringThing3 getString2=%@\n",
[stringThing3 getString1], [stringThing3 getString2]);
stringThing3=[e nextObject];
NSLog(@"\n\nstringThing3 should now equal stringThing1\n");
NSLog(@"stringThing3 getString1=%@\tstringThing3 getString2=%@\n",
[stringThing3 getString1], [stringThing3 getString2]);
stringThing3=[e nextObject];
NSLog(@"\n\nstringThing3 should now equal stringThing2\n");
NSLog(@"stringThing3 getString1=%@\tstringThing3 getString2=%@\n",
[stringThing3 getString1], [stringThing3 getString2]);
[e release];
NSLog(@"e has just been released. This is the new output:\n");
NSLog(@"stringThing1 getString1=%@\tstringThing1 getString2=%@\n",
[stringThing1 getString1], [stringThing1 getString2]);
NSLog(@"stringThing2 getString1=%@\tstringThing2 getString2=%@\n",
[stringThing2 getString1], [stringThing2 getString2]);
NSLog(@"stringThing3 getString1=%@\tstringThing3 getString2=%@\n",
[stringThing3 getString1], [stringThing3 getString2]);
[testDict release];
[stringThing1 release];
[stringThing2 release];
[stringThing3release];
}
// and this is StringThing.h (the methods are pretty self explanatory, so
I'll just use the header):
#import <Foundation/Foundation.h>
@interface StringThing : NSObject {
NSString * string1;
NSString * string2;
}
-(NSString *) getString1;
-(NSString *) getString2;
-(StringThing *)initWithStrings:(NSString *)aString string2:(NSString *)
otherString;
@end
//here is the output
2002-01-31 23:49:52.878 Testing[584] *** _NSAutoreleaseNoPool(): Object
0x72120 of class NSIdEnumerator autoreleased with no pool in place - just
leaking
2002-01-31 23:49:52.879 Testing[584] e has just been made. This is the
normal output:
2002-01-31 23:49:52.880 Testing[584] stringThing1 getString1=Foo
stringThing1 getString2=bar
2002-01-31 23:49:52.881 Testing[584] stringThing2 getString1=Snafu
stringThing2 getString2=Dyah!
2002-01-31 23:49:52.881 Testing[584] stringThing3 getString1=(null)
stringThing3 getString2=(null)
2002-01-31 23:49:52.881 Testing[584]
stringThing3 should now equal stringThing1
2002-01-31 23:49:52.881 Testing[584] stringThing3 getString1=(null)
stringThing3 getString2=(null)
2002-01-31 23:49:52.881 Testing[584]
stringThing3 should now equal stringThing2
2002-01-31 23:49:52.881 Testing[584] stringThing3 getString1=Foo
stringThing3 getString2=bar
2002-01-31 23:49:52.881 Testing[584] e has just been released. This is the
new output:
2002-01-31 23:49:52.882 Testing[584] stringThing1 getString1=Foo
stringThing1 getString2=bar
2002-01-31 23:49:52.941 Testing[584] stringThing2 getString1=Snafu
stringThing2 getString2=Dyah!
2002-01-31 23:49:52.941 Testing[584] stringThing3 getString1=Foo
stringThing3 getString2=bar
Testing.app has exited with status 0.
Sorry if this is off-topic. I've been pulling what little hair I have out
for three days because of this, and I just needed to vent.
If there is something I did to make this happen, PLEASE, let me know. I
have an undented refrigerator down here, and I want to make it blend in.
Thanks!
--
Isaac Sherman
MotaSoft Software (forthcoming)