-2

I guess it's really simple, but I can't find the way to work this out...

@property (strong, nonatomic) NSMutableArray *randomQuestionNumberArray;

I have a method beginning like this

- (int)showQuestionMethod:(int)number;

In this method I have a loop where I fill NSMutableArray with numbers and then shuffle it.

//Creating random questions array
_randomQuestionNumberArray = [[NSMutableArray alloc] init];
for (int i = 0; i < numberOfQuestions; i++) {
    [_randomQuestionNumberArray addObject:[NSNumber numberWithInt:i]];
}

//Shuffling the array
NSUInteger count = [_randomQuestionNumberArray count];
for (NSUInteger i = 0; i < count; ++i) {
    // Select a random element between i and end of array to swap with.
    int nElements = count - i;
    int n = (arc4random() % nElements) + i;
    [_randomQuestionNumberArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}

And this works pretty well. Let's say it shuffles the numbers like 4, 5, 1, 3, 6, 0, 2.

Now in viewDidLoad I try to call the the method showQuestionMethod with the first value of _randomQuestionNumberArray which should be 4 in this case.

[self showQuestionMethod:[_randomQuestionNumberArray[0] intValue]];

The problem is that the method gets passed the value of 0 all the time when it should be 4, but NSLog(@"first value is %@", _randomQuestionNumberArray[0]) returns the correct value of 4.

How do I get around this and convert id type to int?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Domas
  • 39
  • 2
  • 7

2 Answers2

1

You can try this:

[self showQuestionMethod:[(NSNumber)_randomQuestionNumberArray[0] intValue]];
Stefan
  • 5,203
  • 8
  • 27
  • 51
  • This is completely equivalent to the existing code (assuming you change the incorrect `(NSNumber)` to be `(NSNumber *)`); it will not solve the problem. – jscs Mar 08 '14 at 21:33
  • Used type 'NSNumber' where arithmetic or pointer type is required – Domas Mar 08 '14 at 21:34
  • Yes you are right the * is missing and it makes no difference- I've posted a new answer. The code sample was to big for a comment. – Stefan Mar 08 '14 at 22:10
0

Are you sure, that the wrong value is passed? Maybe you have a problem with your NSLog statement in your showQuestionMethod.

I've tried this code and it works fine:

    - (IBAction)start:(id)sender {
NSMutableArray *_randomQuestionNumberArray;
int numberOfQuestions=5;


_randomQuestionNumberArray = [[NSMutableArray alloc] init];
for (int i = 0; i < numberOfQuestions; i++) {
    [_randomQuestionNumberArray addObject:[NSNumber numberWithInt:i]];
}

//shuffling the array
NSUInteger count = [_randomQuestionNumberArray count];
for (NSUInteger i = 0; i < count; ++i) {
    // Select a random element between i and end of array to swap with.
    int nElements = count - i;
    int n = (arc4random() % nElements) + i;
    [_randomQuestionNumberArray exchangeObjectAtIndex:i withObjectAtIndex:n];
}
NSLog(@"first value is %@", _randomQuestionNumberArray[0]);
[self showQuestionMethod:[_randomQuestionNumberArray[0] intValue]];  

}

- (void)showQuestionMethod:(int)number {
NSLog(@"number is %d", number);

}

Stefan
  • 5,203
  • 8
  • 27
  • 51