NSArray如何在未购买应用程序时限制数组中的数据?

时间:2013-05-03 00:15:30

标签: iphone ios xcode ipad

我有一个应用程序,总共有32个随机出现的图像;

我希望这样,如果用户没有购买特定升级,那么只会显示阵列中的前16个(仅限f1.png - f16.png),但是如果他们已经购买了该产品,然后所有32个随机可用。

允许所有图片的应用内ID;

com.myphotofun.upgrade16more &安培; com.myphotofun.complete

调用随机数组的代码是;

-(void)randImage
{
    NSArray *myImageNames = [NSArray arrayWithObjects:@"f1.png", @"f2.png", @"f3.png",@"f4.png",@"f5.png",@"f6.png",@"f7.png",@"f8.png",@"f9.png",@"f10.png",@"f11.png",@"f12.png",@"f13.png",@"f14.png",@"f15.png",@"f16.png","f17.png","f18.png","f19.png","f20.png","f21.png","f22.png","f23.png","f24.png","f25.png","f26.png","f27.png","f28.png","f29.png","f30.png","f31.png","f32.png",nil];

           int index = arc4random() % [myImageNames count];

    UIImage *myImage = [UIImage imageNamed:[myImageNames objectAtIndex:index]];

    [PaintingImage setImage:myImage];
}

现在改为(感谢guenis);

-(void)randImage
{

NSInteger index = arc4random_uniform([[NSUserDefaults standardUserDefaults] boolForKey:PRODUCT_PURCHASED] ? 32 : 16);

UIImage *myImage = [UIImage imageNamed:[NSString stringWithFormat:@"f%d.png",index +1]];

    [PaintingImage setImage:myImage];

}

非常感谢任何代码协助。 谢谢,

克里斯

2 个答案:

答案 0 :(得分:0)

-(void)randImage
{
    NSArray *myImageNames = [NSArray arrayWithObjects:@"f1.png", @"f2.png", @"f3.png",@"f4.png",@"f5.png",@"f6.png",@"f7.png",@"f8.png",@"f9.png",@"f10.png",@"f11.png",@"f12.png",@"f13.png",@"f14.png",@"f15.png",@"f16.png","f17.png","f18.png","f19.png","f20.png","f21.png","f22.png","f23.png","f24.png","f25.png","f26.png","f27.png","f28.png","f29.png","f30.png","f31.png","f32.png",nil];

    NSInteger index = arc4random_uniform([[NSUserDefaults standardUserDefaults] boolForKey:PRODUCT_PURCHASED] ? 32 : 16);

    UIImage *myImage = [UIImage imageNamed:[myImageNames objectAtIndex:index]];

    [PaintingImage setImage:myImage];
}

购买产品时不要忘记设置值

[[NSUserDefaults standardUserDefaults] setBool:YES forKey:PRODUCT_PURCHASED];
[[NSUserDefaults standardUserDefaults] synchronize];

此外,如果您的图像的名称与您显示的完全相同,我宁愿使用动态字符串访问它们,而不是将它们存储在nsarray中:

    NSInteger index = arc4random_uniform([[NSUserDefaults standartUserDefaults] boolForKey:PRODUCT_PURCHASED] ? 32 : 16);

    UIImage *myImage = [UIImage imageNamed:[NSString stringWithFormat:@"f%d.png",index + 1]];

答案 1 :(得分:0)

您好,您可以尝试使用此

    - (void)randImage {

    int items = isPurchased ? 32 : 15;

    NSMutableArray *images = [[NSMutableArray alloc] init];

    for (int i = 1; i <= items; i++) {
        [images addObject:[NSString stringWithFormat:@"f%i.png", i]];
    }

    int index = arc4random() % [images count];

    UIImage *myImage = [UIImage imageNamed:[images objectAtIndex:index]];
    [PaintingImage setImage:myImage];

    //Check if it works
    NSLog(@"%@", [images objectAtIndex:index]);
    NSLog(@"%i", images.count);
}

已经过测试

祝你好运