使用字符串按名称指定数组.目标C.



我正在尝试使用循环来打印出 26 个数组的内容,而不是依次打印出每个数组。
我遇到的问题出在下面的 NSLog 中,我在其中调用特定阵列。 我正在尝试使用"stringName"字符串来指定我要计数的数组。

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

我知道只是将字符串名称拖放到寻找数组的位置是行不通的,但为了清楚起见,这似乎是最好的。 我尝试了许多变化和选项。

我知道这可能不是一个好方法,但我是如此接近! 任何帮助将不胜感激。

谢谢~史蒂夫

你真的应该改变你的方法。与其为数组提供 26 个不同的属性,不如拥有一个数组数组,或者一个字典,其中键是字母,值是关联的数组(选择取决于您使用它们的目的)。

如果将数据模型更新为此,则在现有代码中应该更容易使用,并且当前的问题变得微不足道(记录容器将记录内容)。

最新更新