0

I am trying to use a loop to print out the contents of 26 arrays, rather than printing out each in turn.
The problem I am having is in the NSLog below where I call the specific array. I am trying to use the "stringName" string to specify which array I want to count.

….list of arrays starting at beginWithA….
 NSArray *beginWithT = [randomList filteredArrayUsingPredicate:tPredicate];
    NSArray *beginWithU = [randomList filteredArrayUsingPredicate:uPredicate];
    NSArray *beginWithV = [randomList filteredArrayUsingPredicate:vPredicate];
    NSArray *beginWithW = [randomList filteredArrayUsingPredicate:wPredicate];
    NSArray *beginWithX = [randomList filteredArrayUsingPredicate:xPredicate];
    NSArray *beginWithY = [randomList filteredArrayUsingPredicate:yPredicate];
    NSArray *beginWithZ = [randomList filteredArrayUsingPredicate:zPredicate];

NSString *alphabet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";  

    for (int i =0; i<26; i++)
    {
        unichar c = [alphabet characterAtIndex:i];
        NSString *stringName = [NSString stringWithFormat:@"beginWith%C", ch];
        NSLog (@"The %C Array contains %lu words.", [stringName count]);

    }

I know that just dropping the string name into the spot thats looking for an array won't work but for the sake of clarity here this seemed best. Ive tried many variations and options.

I understand that this may not be a good way to go, but I'm so close! Any help would be greatly appreciated.

thanks ~Steve

  • 2
    This is what arrays are for. – Hot Licks Jan 19 '15 at 22:14
  • See also [Create multiple variables based on an int count](http://stackoverflow.com/q/2231783), [Syntax help: variable as object name](http://stackoverflow.com/q/7940809) [Is it possible to reference a variable with a string and an int?](http://stackoverflow.com/q/6049175) – jscs Jan 20 '15 at 08:10
  • So that thread basically says to use a dictionary. I think I'm seeing a pattern. -The Dictionary object is designed for this type of use whereas an array is not. – Steve Maietta Jan 20 '15 at 12:47

1 Answers1

2

You should really change your approach. Rather than having 26 different properties for the arrays you should have an array of arrays, or a dictionary where the keys is the 'letter's and the values are the associated arrays (the choice depends on what you're using them for).

If you update your data model to this it should be easier to use in your existing code and your current problem becomes trivial (logging the container will log the contents).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • Thanks for the NSDictionary direction, I was thinking about that as a better approach. Will give it a shot. I'd still like to know though, how can I use a string to specify a certain array? So as I set up stringWithFormat, it uses the resulting string to find the matching array. Gotta be possible..? – Steve Maietta Jan 20 '15 at 12:45
  • Found another variant of this question, also specifying the dictionary as the way to go. Allright, case closed. Thanks Stack Overflow. http://stackoverflow.com/questions/6390754/using-nsstring-contents-as-nsarray-name – Steve Maietta Jan 20 '15 at 12:54
  • Technically there is a way, but you won't see many people digging around in the obj-c language underpinnings to achieve it... – Wain Jan 20 '15 at 13:03