0

I am new to Objective-C and learning by trial and error! Please forgive me if this question is somewhat naïve.

I've created an array of images and need to shuffle them. I've used the advice given here:

What's the Best Way to Shuffle an NSMutableArray?

And now I need to implement the method 'shuffle'. I have separate category for my array and its implementation:

@interface theKid : NSObject {
    NSMutableArray* _cards;
}

@property(strong, nonatomic, readonly) NSMutableArray *cards;

- (UIImage*) shuffledCard;

@end

But now I can't figure out how to implement 'shuffle':

@implementation theKid

- (NSMutableArray *) cards {

    _cards = [[NSMutableArray alloc] initWithObjects:
          [UIImage imageNamed:@"One"],
          [UIImage imageNamed:@"Two"],
          [UIImage imageNamed:@"Three"],
          [UIImage imageNamed:@"Four"], nil];

    return _cards;

}

- (UIImage*) shuffledCard {
    shuffle *cards;

    return [cards objectAtIndex:0];
}

@end

This just effects a blank screen when I try to call 'shuffledCard'. Any advice much appreciated.

Community
  • 1
  • 1
  • This code shouldn't even compile. – vikingosegundo Jul 11 '14 at 12:26
  • Hence my request for help. No reason to vote down my question. – Bronny Reinhardt Jul 11 '14 at 12:58
  • You are not shuffling anything. You do deserve a negative vote. – El Tomato Jul 11 '14 at 13:15
  • It's pretty obvious what I want to do. This wasn't a sloppy question. I know that this code doesn't compile – I'm trying to get it right. Just because I'm far off the mark – as a beginner – doesn't mean I shouldn't ask my question. – Bronny Reinhardt Jul 11 '14 at 13:48
  • Stackoverflow isn't an online tutored course. And it isn't a replacement for own effort. Also your questions text is misleading: you say white screen appears, this implies it is running and therefor compiling. – vikingosegundo Jul 11 '14 at 15:17
  • 1
    Actually I've gone to a lot of effort to find out how to shuffle my array of images. This is my last resort. I couldn't figure out how to implement the 'shuffle' method, as I stated. Others had a go at answering my question – they understood it. You're just being supercilious. Maybe you'd like to ban all noobs? If stuff like this offends you, take your smug 'tude and move on, buddy. – Bronny Reinhardt Jul 12 '14 at 01:55
  • As for it compiling, it *did* compile (excuse my misuse of the word above, I should have said 'work'). My app launched but went to a blank screen when I tried to shuffle. – Bronny Reinhardt Jul 12 '14 at 02:05
  • http://stackoverflow.com/search?q=NSArray+shuffle – vikingosegundo Jul 16 '14 at 06:46
  • In fact: Yes. I would like to ban people that aren't able to do basic research, as stack overflow is drowning in the same questions over and over again, written by people to lazy to read basic docs and use search boxes. – vikingosegundo Jul 16 '14 at 06:50

2 Answers2

1

Hi i have pasted few lines of code below which will Shuffle an NSMutable Array exactly like what you want.

NSMutableArray *  _cards = [[NSMutableArray alloc] init];
[_cards addObject:[UIImage imageNamed:@"One.png"]];
[_cards addObject:[UIImage imageNamed:@"Two.png"]];
[_cards addObject:[UIImage imageNamed:@"Three.png"]];
[_cards addObject:[UIImage imageNamed:@"Four.png"]];
[_cards addObject:[UIImage imageNamed:@"Five.png"]];

// Add the below code in the Shuffle Method

NSUInteger count = [_cards count];
for (NSUInteger i = 0; i < count; ++i)
{
    NSInteger remainingCount = count - i;
    int exchangeIndex = i + arc4random_uniform(remainingCount);
    [_cards exchangeObjectAtIndex:i withObjectAtIndex:exchangeIndex];
    UIImage * image = [_cards objectAtIndex:i];
}
Blisskarthik
  • 1,246
  • 8
  • 20
0

Just use this api for shuffling of object exchangeObjectAtIndex:withObjectAtIndex: Refer this how to shuffle object in NSMutableArray?

Community
  • 1
  • 1
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56