Storing NSMutableDictionary in NSMutableArray
Storing NSMutableDictionary in NSMutableArray
- Subject: Storing NSMutableDictionary in NSMutableArray
- From: Chris <email@hidden>
- Date: Thu, 23 Nov 2006 05:45:56 +0800
Hi, I've trying to save a NSMutableDictionary's instance in a
NSMutableArray instance. The NSLog() stubs I've placed show that the
NSMutableDictionary instance is correctly alloc'ed and initialized.
However, adding it to the NSMutableArray instance will result in only
the key and not the value being stored. The ivar NSMutableArray is
declared in myDocument.h as
@interface MyDocument : NSDocument
{
NSXMLParser *XMLParser;
NSMutableString *currentStringValue;
NSMutableArray *aMutableArray, *nestedTags;
}
and the relevant definition in myDocument.m is
- (IBAction) aButtonWasPressed: (id) sender {
...
aMutableArray = [NSMutableArray array];
[self extractKeyValuePair];
....
}
- (void) extractKeyValuePair {
[XMLParser setDelegate:self];
[XMLParser setShouldResolveExternalEntities:YES];
return [XMLParser parse]; // return value not used; if not
successful, delegate is informed of error
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)
elementName namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName attributes:(NSDictionary *)attributeDict {
[nestedTags addObject:elementName];
[parser setShouldProcessNamespaces:TRUE];
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)
string {
if (!currentStringValue) {
currentStringValue = [[NSMutableString alloc]
initWithCapacity:XML_STRING_MAX_LENGTH];
}
if (string != @"") [currentStringValue appendString:[string
stringByTrimmingCharactersInSet:[NSCharacterSet
whitespaceAndNewlineCharacterSet]]];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)
elementName namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName {
NSMutableDictionary *keyValuePair = [NSMutableDictionary
dictionaryWithCapacity:5];
NSMutableString *currentTag = [NSString stringWithString:
[nestedTags lastObject]];
[nestedTags removeLastObject];
if ([nestedTags containsObject:@"TVProgramID"]) {
[keyValuePair setObject:currentStringValue forKey:currentTag];
NSLog(@"Adding %@", keyValuePair); // Run Log below suggests
NSDictionary correctly created
[aMutableArray addObject:keyValuePair]; // suspect this is
the root of the problem
[currentStringValue setString:@""];
NSLog(@"%@\n", aMutableArray); // Run Log below shows addObject
added the 'key' but not 'value'
}
The NSLog()'s in the Run Log as the program traverses the XML file
shows the NSMutableArray addObject: not storing the keyValuePair
(NSMutableDictionary)
2006-11-22 16:31:16.118 test[8029] Adding {Code =
"{WDSM-3011-20061103}"; }
2006-11-22 16:31:16.119 test[8029] ({Code = ""; })
2006-11-22 16:31:16.120 test[8029] Adding {Name = "TV Channel Program
ID"; }
2006-11-22 16:31:16.120 test[8029] ({Code = ""; }, {Name = ""; })
2006-11-23 04:31:16.120 test[8029] Adding {Episodes = 10; }
2006-11-23 04:31:16.121 test[8029] ({Code = ""; }, {Name = ""; },
{Episodes = ""; })
2006-11-23 04:31:16.121 test[8029] Adding {Rating = "PG-13"; }
2006-11-23 04:31:16.121 test[8029] ({Code = ""; }, {Name = ""; },
{Episodes = ""; }, {Rating = ""; })
I've searched the cocoadev archive and this mailing list archive for
similar cases to no avail. Any suggestions welcomed. Thanks in advance.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins (at) lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden