newbie NSMutable dictionary subclass question
-
Can the value part of a key-value pair in an NSMutableDictionary be a
literal NSString, like @"name"?
It would seem so, because this works or at least doesn't crash:
NSMutableDictionary *bobo;
bobo = [[NSMutableDictionary alloc] init];
[bobo setValue:@"root" forKey:@"name"];
However, I have a class derived from NSMutableDictionary, and it is
crashing when I try to do the same thing:
@interface myClass : NSMutableDictionary {
}
@end
myClass *tree;
tree = [[myClass alloc] init];
[tree setValue:@"root" forKey:@"name"];
Why does that crash when the first does not? Are subclasses required
to have member variables, or can they just have new functions? Mine
don't have any variables, maybe that is the problem?
Thanks for any replies!
Bob -
> Why does that crash when the first does not? Are subclasses required to
> have member variables, or can they just have new functions? Mine don't have
> any variables, maybe that is the problem?
See:
<http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals
/CocoaObjects/chapter_3_section_9.html#//apple_ref/doc/uid/TP40002974-CH4-D
ontLinkElementID_105>
-- Chris -
As a self-professed newbie, you almost certainly shouldn't be
subclassing NSMutableDictionary. Can I ask your reasons for doing so?
On 17 Nov 2008, at 00:25, Bob Sabiston wrote:
> Can the value part of a key-value pair in an NSMutableDictionary be
> a literal NSString, like @"name"?
>
> It would seem so, because this works or at least doesn't crash:
>
> NSMutableDictionary *bobo;
> bobo = [[NSMutableDictionary alloc] init];
> [bobo setValue:@"root" forKey:@"name"];
>
>
> However, I have a class derived from NSMutableDictionary, and it is
> crashing when I try to do the same thing:
>
>
> @interface myClass : NSMutableDictionary {
> }
> @end
>
> myClass *tree;
> tree = [[myClass alloc] init];
> [tree setValue:@"root" forKey:@"name"];
>
>
> Why does that crash when the first does not? Are subclasses
> required to have member variables, or can they just have new
> functions? Mine don't have any variables, maybe that is the problem?
>
> Thanks for any replies!
> Bob
-
> Can the value part of a key-value pair in an NSMutableDictionary be
> a literal NSString, like @"name"?
>
> It would seem so, because this works or at least doesn't crash:
>
> NSMutableDictionary *bobo;
> bobo = [[NSMutableDictionary alloc] init];
> [bobo setValue:@"root" forKey:@"name"];
>
>
> However, I have a class derived from NSMutableDictionary, and it is
> crashing when I try to do the same thing:
>
>
> @interface myClass : NSMutableDictionary {
> }
> @end
>
> myClass *tree;
> tree = [[myClass alloc] init];
> [tree setValue:@"root" forKey:@"name"];
>
>
> Why does that crash when the first does not? Are subclasses
> required to have member variables, or can they just have new
> functions? Mine don't have any variables, maybe that is the problem?
>
>>
Look like someone that did not read the fondamental before coding ;-)
Short answer: because NSDictionary is a class cluster.
Long answer here:
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals
/CocoaObjects/chapter_3_section_9.html



