在 Obj-C 中随机填充队列
现在我正在设置一个基于图像名称的队列,该队列运行良好。它循环遍历图像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
做这样的事情。
而且你的代码不应该完美地工作。您正在泄漏
imageName
。Do something like this.
And your code should not work flawlessly. You are leaking
imageName
.我会首先用图像名称填充一个数组,然后随机挑选值:
I would do it by first populating an array with the image names, and then randomly picking out values: