1

while adding a integer to object at index of an array, I am getting an error of "arithmetic on pointer to interface id which is not a constant size for this architecture and platform", didn't know how to resolve it. Please help.

my code is -

 if (arrayTotalAmount.count>0) {
                     int sum = 0;
                    for (int i = 0; i<=arrayTotalAmount.count; i++) {
                     sum = (sum+[arrayTotalAmount objectAtIndex:i]);

    }

In 4th line I am getting that error. Thanks

2 Answers 2

2

Objective C array only accepts NSObject type. That means it is impossible to insert a primitive value into an NSArray. You are getting an error because objectAtIndex method returns a pointer which points to that NSObject, the arithmetic operations are still valid on pointers but the thing is that the size of a pointer as integer (32bit, 64bit) may differ on device. So one of the solution is typecasting the pointer sum+(int)[arrayTotalAmount objectAtIndex:i] which makes no sense in your case.

The solution you are looking for is probably sum+[[arrayTotalAmount objectAtIndex:i] intValue] or similar. Assuming that array contains NSNumber objects. If the object inside an array is not an NSNumber then your app will fail in runtime showing an error indicating that an object X does not have a method called intValue in which case you will need to figure how to convert object X to your int.

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

2 Comments

Thanks for your valuable time. I found my answer :)
@Harsh It is the same solution as the other answer. But just since you are working with arrays in Objective-C note that instead of objectAtIndex:i you can use [arrayTotalAmount[i] intValue]. But in your case it is better to just turn the loop around and use for(NSNumber *number in arrayTotalAmount) sum += [number intValue];
1

You just need to convert your array object to integer and then add it will work for you.

if (arrayTotalAmount.count>0) {
                     int sum = 0;
                    for (int i = 0; i<=arrayTotalAmount.count; i++) {
                     sum = (sum+[[arrayTotalAmount objectAtIndex:i] intValue]);

    }

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.