开发者

iphone Development - pick unique image from array problem, syntax help

Code:

//pick one filename
int numFileNames = [imageArray count];
int chosen = arc4random() % numFileNames;
NSString *oneFilename = [image开发者_运维知识库Array objectAtIndex: chosen];

thanks!!


There's one error in your NSMutableArray:

You need to initialize it first.

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

;)

and here's a suggestion, if you do not necessarily require the interface builder's assistance, you might want to consider cocos2d. The stuffs you wanted to do can be done easily in it.

-- as for step 3. Do a random pick using Rand(), make a loop to check if the selected image is already added in an array (this array is for picked images), if it's in the array randomize again, if not then add in into the picked array, and do ball1.image = [UIImage imageNamed:[imageArray objectatindex:randomNum]];


The question does not specify the issue at hand, it just mentions that the developer wants to store a unique image name in array, so coding can be done:

NSMutableArray *arrImages = [NSMutableArray new];

for (int i=0; i<2; i++) {

    NSString *imgName = [NSString stringWithFormat:@"imageBall%d",i];

    //Here we can check whether image is already added or not.
    if (![arrImages contains Object:imgName]) {
        [arrImages addObject:imgName];
    }
}

int choosen = arc4random() % (int)arrImages.count;

NSString *imageFileName = arrImages[choosen];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜