在 Obj-C 中随机填充队列

发布于 2024-11-25 20:17:52 字数 577 浏览 1 评论 0原文

现在我正在设置一个基于图像名称的队列,该队列运行良好。它循环遍历图像 0 到 13 并将它们添加到队列中。

loadImagesOperationQueue = [[NSOperationQueue alloc] init];

NSString *imageName;
for (int i=0; i < 13; i++) {
    imageName = [[NSString alloc] initWithFormat:@"cover_%d.jpg", i];
    [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
    NSLog(@"%d is the index",i);

}

这工作完美;队列设置为从 cover_0.jpg 到 cover_13.jpg。不过,我想为其添加一点随机性。如果我只使用 arc4random() ,毫无疑问我会多次将相同的图像添加到队列中。从逻辑上讲,我怎样才能使 arc4random() 具有独占性。将所选数字添加到字符串中,然后根据当前输出检查它们,如果需要重复 arc4,这是多余且低效的。

Right now I am setting up a queue based off of image names that works fine. It loops through images 0 through 13 and adds them to the queue.

loadImagesOperationQueue = [[NSOperationQueue alloc] init];

NSString *imageName;
for (int i=0; i < 13; i++) {
    imageName = [[NSString alloc] initWithFormat:@"cover_%d.jpg", i];
    [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
    NSLog(@"%d is the index",i);

}

This works flawlessly; the queue is set up from cover_0.jpg through cover_13.jpg. I want to add a little randomness to it, however. If I just use an arc4random() I will undoubtedly get the same image added to the queue multiple times. Logically, how can I get arc4random() to be exclusive. Adding the chosen numbers to a string and then checking them against the current output, and if needed repeating the arc4, is redundant and inefficient.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

猫卆 2024-12-02 20:17:52

做这样的事情。

NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithCapacity:14];

for (int i = 0; i < 13; i++) {
    [tmpArray addObject:[NSString stringWithFormat:@"cover_%d.jpg", i]];
}

for (int i = 0; i < 13; i++) {
    int index = arc4random() % [tmpArray count];
    NSString *imageName = [tmpArray objectAtIndex:index];
    [tmpArray removeObjectAtIndex:index];
    [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
}

[tmpArray release];

而且你的代码不应该完美地工作。您正在泄漏imageName

Do something like this.

NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithCapacity:14];

for (int i = 0; i < 13; i++) {
    [tmpArray addObject:[NSString stringWithFormat:@"cover_%d.jpg", i]];
}

for (int i = 0; i < 13; i++) {
    int index = arc4random() % [tmpArray count];
    NSString *imageName = [tmpArray objectAtIndex:index];
    [tmpArray removeObjectAtIndex:index];
    [(AFOpenFlowView *)self.view setImage:[UIImage imageNamed:imageName] forIndex:i];
}

[tmpArray release];

And your code should not work flawlessly. You are leaking imageName.

可爱咩 2024-12-02 20:17:52

我会首先用图像名称填充一个数组,然后随机挑选值:

NSMutableArray * imageNames = [NSMutableArray array];
for (int i = 0; i < 13; i++) {
    NSString * iName = [NSString stringWithFormat:@"cover_%d.jpg", i];
    [imageNames addObject:iName];
}
while ([imageNames count] > 0) {
    int index = arc4random() % [imageNames count];
    NSString * iName = [imageNames objectAtIndex:index];
    [imageNames removeObjectAtIndex:index];
    // load image named iName here.
}

I would do it by first populating an array with the image names, and then randomly picking out values:

NSMutableArray * imageNames = [NSMutableArray array];
for (int i = 0; i < 13; i++) {
    NSString * iName = [NSString stringWithFormat:@"cover_%d.jpg", i];
    [imageNames addObject:iName];
}
while ([imageNames count] > 0) {
    int index = arc4random() % [imageNames count];
    NSString * iName = [imageNames objectAtIndex:index];
    [imageNames removeObjectAtIndex:index];
    // load image named iName here.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文