1

I read a book on Objective-C and made ​​a couple of simple applications, but still can't figure out how to do some easy things. For example, there are ten names of books and ten covers to them.I want make appear one random book name on the screen with it's cover. I can write something like this:

NSArray* bookNames = @[@"Harry Potter", @"Atlas Shrugged", @"The Financier"];
NSString* book= bookNames[arc4random()%bookNames.count];

Is it right? How can I load an image of cover to UIImageView depending on book name?

3 Answers3

1

You need another NSArray where you have to store names of your images, on the same index as your bookNames. For example:

//Your Arrays bookNames for names and bookImages for name of your images
NSArray* bookNames = @[@"Harry Potter", @"Atlas Shrugged", @"The Financier"];
NSArray* bookImages = @[@"Harry-Potter.jpg", @"Atlas-Shrugged.jpg", @"The-Financier.jpg"];

//Setting a randomindex for your book
NSInteger yourRandomIndex = arc4random_uniform(bookNames.count);

//Getting your imageName and your bookName for your book at randomIndex
NSString* bookName= bookNames[yourRandomIndex];
NSString* bookImage= bookImages[yourRandomIndex];

//Setting your ImageView image and your bookName in a label from your strings
[imageView setImage:[UIImage imageNamed:bookImage]];
bookName.text = bookName;
E-Riddie
  • 14,660
  • 7
  • 52
  • 74
  • 2
    and for better randomness use `arc4random_uniform(bookNames.count)` instead of `arc4random()%` to avoid [modulo bias](http://stackoverflow.com/questions/10984974/why-do-people-say-there-is-modulo-bias-when-using-a-random-number-generator/10984975#10984975), which in case you have three books will be quite high – Matthias Bauch Jun 11 '14 at 18:29
  • @MatthiasBauch thnx for pointing out about generating numbers, I have never known about that was an issue! Answer updated :) – E-Riddie Jun 11 '14 at 18:36
  • @3r1d Xcode says "Incompatible integer to pointer conversion initializing 'NSInteger' (aka "int *") with an expression of type 'u_int32_t' (aka 'unsigned int')". What does it mean? –  Jun 12 '14 at 18:01
  • 1
    #vinand I accidentally added pointer to `NSInteger *yourRandomIndex`, change it back to `NSInteger yourRandomIndex` – E-Riddie Jun 12 '14 at 18:07
  • @3r1d One more thing: when I changed NSArray to NSMutableArray,Xcode showed warning. Why ? –  Jun 13 '14 at 18:31
  • 1
    That's because NSArray has not mutable data while NSMutableArray has. You need to init the NSMutableArray or add mutablecopy to your NSMutableArray like this [NSArray mutableCopy] – E-Riddie Jun 13 '14 at 18:52
1

In cases like these, I usually create a (small) class, let's call it Books, with two properties:

@property (nonatomic, strong) NSString *bookTitle;
@property (nonatomic, strong) NSString *bookImage;

Create a static method that returns a new book object:

+ (Books *)createBookWithTitle:(NSString *)title andImage:(NSString *)image
{
    Books *book = [[Books alloc]init];

    book.bookTitle = title;
    book.bookImage = image;

    return book;
}

Then, you create an array, and add Books objects to it:

NSArray *books = @[[Books createBookWithTitle:@"Harry Potter" andImage:@"HarryPotter.png"],
                   [Books createBookWithTitle:@"Atlas Shrugged" andImage:@"Atlas Shrugged.png"],
                       ];

Then, you can just use:

Books *selectedBook = books[randomNumber];

NSString *title = selectedBook.bookTitle;
UIImage *image = selectedBook.bookImage;

It looks like overkill, but it clean and easy to work with. They pair very nicely that way.

bauerMusic
  • 5,470
  • 5
  • 38
  • 53
  • 1
    I use beans too, but in this case we are dealing with just two properties. If OP needs to start learning dealing with multiple properties he should start learning beans, and you provided a nice answer. Vote up! – E-Riddie Jun 11 '14 at 18:41
0
UIImageView *yourImageView = ...
    NSArray* bookNames = @[@"Harry Potter", @"Atlas Shrugged", @"The Financier"];
NSString* book= bookNames[arc4random()%bookNames.count];
[yourImageView setImage:[UIImage imageNamed:book]];

Hope this helps

David Xu
  • 5,555
  • 3
  • 28
  • 50