2

i'm from poland so sorry for word mistakes.

what's my problem?

i've got array with representation of red color, its:

NSMutableArray *redColor = [[NSMutableArray alloc] initWithCapacity: 255];

i've got FOR loop for fill my array value: 0.

for (int i=0; i < 255; i++) {
    [redArray insertObject:[NSNumber numberWithInt:0] atIndex:i];
}

ok it's fine. but problem is when i want to add value in next FOR loop.

for (NSUInteger ii = 0 ; ii < width * height ; ++ii)
{
 //blablabla


[redArray insertObject:[NSNumber numberWithInt:redArray[ii]+1] atIndex:redArray[ii]];
//example: ii=4
//insertObject: (numberWithInt:value for redArray[4] + 1) at index:4.

//it's not work because i've got ERROR: Subscript requiers size of interface
//"NSMutableArray", which is non constant in non-fragile ABI.
}

how i can fix it? i don't know how to easy increment value in NSMutableArray cell.

thanks you for help, it's my first post here.

regards, Tomek

2 Answers 2

3

Unlike C/C++, NSArray is not accessed using a [] operator. To access an element of an NSArray, use objectAtIndex:.

[redArray objectAtIndex:ii]

Additionally, when you want to add 1 to the element in the array, you must convert the original value back to an int from NSNumber.

[[redArray objectAtIndex:ii] intValue] +1

Alternatively, you could just use a normal C array.

int redArray[255];
redArray[index] = redArray[index] + 1

Objective-C is a super-set of C (meaning it contains all of C), so standard C arrays are valid.

Good luck!

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

Comments

2

There are three problems with your code. First, you cannot access array elements with the subscript syntax. To access objects contained in a NSArray, you need to use the objectAtIndex: method.

You need to replace:

redArray[ii]

with:

[redArray objectAtIndex:ii]

And then you'll get a NSNumber instance, with which you can't directly do arithmetic. You'll need to call intValue on it first:

[[redArray objectAtIndex:ii] intValue] + 1

Also, the atIndex: parameter of insertObject:atIndex: accepts a NSInteger, not a NSNumber. Therefore, it makes no sense to query the NSArray. You will have to replace:

redArray[ii]

with just:

ii

This call will insert a new object in the array. In case you meant to replace the previous NSNumber, use replaceObjectAtIndex:withObject:.

With all the changes, the line should look like this (I've split it on several lines for the sake of clarity):

int red = [[redArray objectAtIndex:ii] intValue] + 1;
NSNumber* redObject = [NSNumber numberWithInt:red];
[redArray insertObject:redObject atIndex:ii];
// or possibly:
// [redArray replaceObjectAtIndex:ii withObject:redObject];

1 Comment

One little correction: insertObject:atIndex: is a method (instance method) for NSMutableArray, not NSArray.

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.