0

I have a NSMutableArray that I want to edit. I want to check if all the members belong to class QueueMember (which has a property rank), and if it does I want it to set the value of rank for each object to its index in the Array.

This is what I have now which does not work. I'm wondering if it doesn't work because *queue is a pointer?

-(void)makingRankEqualtoLocationInQueue:(NSMutableArray *)queue{
    queue = [[NSMutableArray alloc]init];
    for (int queueSizeCounter = 0; queueSizeCounter <queue.count; queueSizeCounter++) {
        id obj = [queue objectAtIndex:queueSizeCounter];

        if ([obj isKindOfClass:[QueueMember class]]) {
            QueueMember *member = obj;
            member.rank = queueSizeCounter;
            [queue replaceObjectAtIndex:queueSizeCounter withObject:member];
        } else {
            [queue removeObjectAtIndex:queueSizeCounter];
        }
    }
}
1
  • also, im pretty new but is it necessary I still alloc/init the queue within the method in xcode5 despite declaring it already? Commented Feb 21, 2014 at 2:17

2 Answers 2

1

Here's your problem:

-(void)makingRankEqualtoLocationInQueue:(NSMutableArray *)queue{
    queue = [[NSMutableArray alloc]init];

So, maybe an array arrives as a parameter (queue in the first line) but you then throw it away and replace it by a completely different new empty array (queue in the second line). So now you have an empty array, its count is zero, and the loop never runs.

Sign up to request clarification or add additional context in comments.

Comments

1

I agree with matt, but that's not the only problem.

First, here:

QueueMember *member = obj;
member.rank = queueSizeCounter;
[queue replaceObjectAtIndex:queueSizeCounter withObject:member];

the replaceObject is unnecessary. member points to the object in the array, and if you change it, it's changed. :)

Second, are you intending to change the passed queue (by removing non-QueueMembers)? If so, maybe that side effect should be more obvious in your method naming. If not, the removeObjectAtIndex: is hurting you.

Finally, I don't know if this is intentional, but you'll end up with ranking that includes the non-QueueMember objects, perhaps something like ranks [1, 2, 5, 7, 13].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.