• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Storing NSMutableDictionary in NSMutableArray
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Storing NSMutableDictionary in NSMutableArray


  • Subject: Re: Storing NSMutableDictionary in NSMutableArray
  • From: "stephen joseph butler" <email@hidden>
  • Date: Wed, 22 Nov 2006 16:54:52 -0600

2006/11/22, Chris <email@hidden>:
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

You've got several problems here...

@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];
....
}

You need to retain aMutableArray, otherwise it is deallocated at the end of this pass through the run loop.

- (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]]];
}

You can't compare NSString's this way, as far as I know. What you're doing is comparing pointer values, which will probably never work. Try [string isEqualToString:@""].

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)
elementName namespaceURI:(NSString *)namespaceURI qualifiedName:
(NSString *)qName {
         NSMutableDictionary *keyValuePair = [NSMutableDictionary
dictionaryWithCapacity:5];
     NSMutableString *currentTag = [NSString stringWithString:
[nestedTags lastObject]];

You've stored an NSString in an NSMutableString instance? Doesn't this give a compiler warning, or something?

If you wanted a mutable string, the best way would be to do:

currentTag = [[[nestedTags lastObject] mutableCopy] autorelease];


     [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'
}

(Ignoring that your aMutableArray may be deallocated at this point....)

When you add a pointer to a collection (keyValuePair) you don't make a
copy. So in line #3 you add it -- suppose ("aKey","aValue") -- then in
line #6 you clear it, giving ("aKey", ""). Which is what I suspect
you're really seeing in line #7. Try [[currentStringValue copy]
autorelease]
_______________________________________________

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


References: 
 >Storing NSMutableDictionary in NSMutableArray (From: Chris <email@hidden>)

  • Prev by Date: Re: Questions for implementing pop-up contextual menus for Ctrl-Click and Right-Click
  • Next by Date: Re: Custom Cocoa Views in Interface Builder
  • Previous by thread: Storing NSMutableDictionary in NSMutableArray
  • Next by thread: Localized numbers 1000 and over not returned
  • Index(es):
    • Date
    • Thread