user7434266
user7434266

Reputation:

how to display the value stored in array by using for loop in objective-c

LOGIC:

example:

apple.png,mango.png,bird.png,sun.png,moon.png

I need to use arc4random in this array, so I got index as 1, so value got mango.png

in other array I stored

number = [[NSMutableArray alloc]initWithObjects:@"4",@"2",@"9",@"1",@"8",@"7",@"5",@"3",@"6",@"10", nil];

and used arc4random and i got the output as index:2 value as:9

then i need to print 9 times image of mango in image view.

please help how to code this using objective-c

Upvotes: 0

Views: 106

Answers (3)

Neha Gupta
Neha Gupta

Reputation: 539

As per my understanding, you have to show random Image in ImageView on the basis of random number.

Have a look below code,

images = [[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"],[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"], nil];

int randomIndex=arc4random() % images.count;
UIImage *selectedImage = [images objectAtIndex:randomIndex]; //random selected image

number = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];
int randomNumber = arc4random() % number.count; //random selected number

NSInteger indexValue = [number indexOfObject:[NSString stringWithFormat:@"%d",randomNumber]]; // get index number of your random number

for (int i = 0; i <indexValue ; i++) {
    NSLog(@"%@", selectedImage);
    yourImageView.Image = selectedImage;
}

Upvotes: 1

Rakesh Tatekonda
Rakesh Tatekonda

Reputation: 91

Important thing is where do you want to display the image.

From the above snippet what I can understand is v(NSArray) is assigned with UIImage. If you want to use v as NSArray then add UIImage as an object to the Array, then you will be able to access image using index.

NSArray *v=[[NSArray alloc] initWithObjects:[imagesArray objectAtIndex:randomIndex], nil];

then you can NSLog the image object with below code

for (int i= 0 ; i<ran; i++) {
    NSLog(@"%@",[v objectAtIndex:0]);
   _cardsImageView.image = [v objectAtIndex:0]
}

Upvotes: 0

Nirav Kotecha
Nirav Kotecha

Reputation: 2581

you have to show number of times images in NSLog as per the random number comes. so just add the following code at the end of your existing code.

int count = [[number objectAtIndex:ran] intValue];
    for(int i=0; i<count; i++)
    {
        NSLog(@"%@",v);
    }

Upvotes: 0

Related Questions