0

I need to dynamically, at run time do the following with a NSMutableDictionary ...

  • create (perhaps thousands) of arrays that are added to the dictionary. This will be done inside a loop and the number of arrays needed will be unknown as they are driven off a plist of words.
  • create (thousands) of strings for each array. Again, data driven, the number of strings is known only at runtime.
  • Use the key to represent some character pattern for the particular array (ex: 'm-m-' means the array contains strings with two m's at the 2nd & 4th location.)
  • Use the value to represent the count of strings that match the pattern (ex:200 strings are of '-m-m' pattern), and use the array to have the actual 200 strings.

    So once implemented, the dictionary will contain a count/value of each pattern/key and the actual strings that match that pattern.

    I need help with the proper syntax for creating dictionary and adding arrays dynamically, and incrementing the count/value, and I suppose appending/inserting the strings but that part I think I am good with.

    related post: NSMutableArray or NSMutableDictionary : which is best for this scenario?

  • 1
    • I updated my answer to your previous post.
      – user523234
      Apr 17, 2012 at 16:41

    1 Answer 1

    1

    I need help with the proper syntax for creating dictionary and adding arrays dynamically, and incrementing the count/value,

    Arrays are objects, and you add them to a dictionary the same way that you'd add any other object to a dictionary:

    [someDictionary setObject:theArray forKey:theKey];
    

    I'm not clear on what you mean about "incrementing the count/value". A dictionary knows how many key/value pairs it contains -- no need to increment anything. Same goes for arrays.

    So it sounds like your code is going to go something like:

    NSMutableDictionary *dict;
    for (NSString *word in wordlist) {
        @autorelease {
            NSMutableArray *wordStrings = [NSMutableArray array];
            while (/*some condition based on word*/) {
                NSString *newString = [NSString stringWithFormat:...];
                [wordStrings addObject:newString];
            }
            [dict setObject:wordStrings forKey:word];
        }
    }
    

    I'm sure that's not quite right -- I have no idea what you're really trying to do, but the code above shows how you'd generate some number of arrays and add them to a dictionary. The rest is up to you.

    Looking at your edit, I think you may be a little confused about what's stored in a dictionary. A dictionary is just a set of key/value pairs. In this case, the values are the arrays that you add, and the keys are the pattern strings. There's no space for an extra "value" for each key that stores a count. You could have two entries for each pattern using some convention for the keys like pattern and *pattern_count*. There's no need to do that, though... the array stored for each pattern can easily tell you how many objects it contains:

    NSArray *wordList = [dictionary objectForKey:pattern];
    int count = [wordlist count];
    
    4
    • Thanks for the clarification, it is helping. Some more info: My task is to get a character from the user(ex:m), then go through the thousands of words and store the strings into pattern categories. ex: m---,-m-m,etc). I am to keep the array that has the highest pattern count and use it as a seed for the next loop. Then get another character (ex:e) and do it again (m---,me--,mee-,m--e), again keep the highest pattern count, until eventually the array is wittled down to only one word. Apr 17, 2012 at 15:47
    • It is a school game project and I don't want to cheat by getting advice here, but I certainly can use help with the syntax for doing this work in this type of loop. I will work with what you gave me above and see where that leads, any other info appreciated. Apr 17, 2012 at 15:47
    • Rather than trying to precompute lists for all possible patterns (that's a lot of patterns!), you might try to find fast ways to generate the one list that you actually need on the fly.
      – Caleb
      Apr 17, 2012 at 15:52
    • Syntax is now working - thank you. Determining the best logic for finding the most words in any given pattern. You are right, there could be several millions of patterns and the goal is really to group together the largest pattern list and throw out others. Not sure how I will do this yet. Apr 17, 2012 at 16:40

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Not the answer you're looking for? Browse other questions tagged or ask your own question.