2

I have the following code:

for (int i = 1; i <= [nmaSpread count];)
{
    [nmaUserName addObjectsFromArray:[nmaSpread objectAtIndex:i]];
    [nmaSpread removeObjectAtIndex:i];
    i += 2;
}

I have declared all variables as global, nmaUserName and nmaSpread are both NSMutableArrays, and have been allocated in viewDidLoad.

I want to store all the odd objects from nmaSpread into nmaUsername and then delete the active object at nmaSpread.

However it keeps crashing with this error:

[NSMutableArray addObjectsFromArray:]: array argument is not an NSArray 2011-12-11 21:08:55.123 appName[15671:f803] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '* -[NSMutableArray addObjectsFromArray:]: array argument is not an NSArray'

3 Answers 3

3

nmaSpread itself is an NSMutableArray, but it looks like the objects it contains aren't. When you do [nmaSpread objectAtIndex:i], that returns an object from nmaSpread. This object isn't an array, so to add it, you'd just use addObject:, not addObjectsFromArray:.

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

Comments

2

There are a few problems with your code.

First off, you've miss-understood what -addObjectsFromArray: does. The actual method you want is just addObject:.

Second, it is dangerous to modify an array while looping through it. The official line from Apple is "this may work in some situations, but don't do it because it might stop working at any point in the future". You need to wait until after you have finished looping through the array, and then delete them. Your current code is definitely doing it wrong.

You could sit down with pen/paper and work out the math to keep it all intact, but it's easier and safer to just do this:

NSMutableIndexSet *indexesToRemove = [NSMutableIndexSet indexSet];

for (int i = 1; i <= [nmaSpread count];)
{
    [nmaUserName addObject:[nmaSpread objectAtIndex:i]];
    [indexesToRemove addIndex:i];
    i += 2;
}

[mmaSpread removeObjectsAtIndexes:indexesToRemove];

1 Comment

Ok thankyou :) I also made an error it should be i += 1 to make the odd number.
1

NSMutableArray's addObjectsFromArray expects you to pass another array, but you are passing a single object (the one at index 'i')

You can try switching

[nmaUserName addObjectsFromArray:[nmaSpread objectAtIndex:i]];

to

[nmaUserName addObject:[nmaSpread objectAtIndex:i]];

and that will remove the error you are seeing, but then you are likely to run into another problem because you are removing objects from nmaSpread as you go and as a result the indexes for items later in the array get shifted. You should probably change your logic around to deal with that problem.

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.