如何使用count方法限制数组中的结果

时间:2013-07-15 20:11:27

标签: ios objective-c nsarray

我目前使用的系统如果用户已升级应用程序,则所有结果都会在彩色图表中查看。

在ViewController.m的顶部

int kMaxRecords = 300;

就在阵列之前;

allText = [[NSMutableArray alloc] init];

然后是300种颜色,如;

[allText addObject:@"Indigo"]; 

依旧......

这是由IBAction控制的;

- (IBAction)setRandomText {
    selectedRecNumber = (arc4random() % kMaxRecords);
    NSString *text = [allText objectAtIndex:selectedRecNumber];
    [randomText setText:text];

if ([MKStoreManager featureAPurchased]) {

        kMaxRecords = [allText count];

    }else{

        // this is where I would like to control a set limit of colors, let's say, 50, or 100

        }

    }

    count++;

}

所以在}之后{我将如何设置,例如,仅显示300的前100种颜色?我尝试过使用;

kMaxRecords = 100;

然而它没有用......

编辑 -

进一步复杂化让我们介绍一些更多的可能性;

 if ([MKStoreManager featureAPurchased] || [MKStoreManager featureDPurchased] ||[MKStoreManager featureEPurchased] ) {

        kMaxRecords = [allText count];

    }else{

            if (![MKStoreManager featureAPurchased] || ![MKStoreManager featureCPurchased] || ![MKStoreManager featureDPurchased] || ![MKStoreManager featureEPurchased] ) {

                kMaxRecords = 35; 
                allText = [[allText subarrayWithRange:NSMakeRange(0, 34)] mutableCopy];

        }else{

            if ([MKStoreManager featureCPurchased] ) {

                kMaxRecords = 135; 
                allText = [[allText subarrayWithRange:NSMakeRange(0, 134)] mutableCopy];

                }
            }

        }

}

编辑2 - 这是viewDidLoad;

- (void)viewDidLoad {

   allText = [[NSMutableArray alloc] init];

    [allText addObject:@"Green"];
    [allText addObject:@"Orange"];
    and more and more...



    if ([MKStoreManager featureAPurchased] || [MKStoreManager featureDPurchased] ||[MKStoreManager featureEPurchased] ) {

        kMaxRecords = [allText count];

    }else{

            if (![MKStoreManager featureAPurchased] || ![MKStoreManager featureCPurchased] || ![MKStoreManager featureDPurchased] || ![MKStoreManager featureEPurchased] ) {

                kMaxRecords = 35; 
                allText = [[allText subarrayWithRange:NSMakeRange(0, 34)] mutableCopy];

        }else{

            if ([MKStoreManager featureCPurchased] ) {

                kMaxRecords = 135; 
                allText = [[allText subarrayWithRange:NSMakeRange(0, 134)] mutableCopy];

                }
            }

        }

}

- (IBAction)setRandomText {

    selectedRecNumber = (arc4random() % kMaxRecords);
    NSString *text = [allText objectAtIndex:selectedRecNumber];
    [randomText setText:text];
    count++;

}

1 个答案:

答案 0 :(得分:0)

您可以使用subarrayWithRange重新定义allText:以包含任意数量的原始项目:

- (void)viewDidLoad {
     [super viewDidLoad];
     allText = [[NSMutableArray alloc] init];
     allText addObject:@"Indigo"];
     //... etc. until array is filled with 300 colors
     [self makeUserArray];
}

-(void)makeUserArray {
    if ([MKStoreManager featureAPurchased]) {
        kMaxRecords = [allText count];
    }else{
        kMaxRecords = 100; // or whatever number you want here
        usableText = [[allText subarrayWithRange:NSMakeRange(0, 100)] mutableCopy];
    }
}

- (IBAction)setRandomText {
    selectedRecNumber = (arc4random() % kMaxRecords);
    NSString *text = [usableText objectAtIndex:selectedRecNumber];
    [randomText setText:text];

    count++;
}