Sorry for the newbie question, but i cannot find an answer to it.
I have a simple operation. I declare a variable, and then i want to loop through an array of integers and add these to the variable. However, i can’t seem to find how to get a += equivalent going in Objective C.
Any help would be awesome.
Code:
NSInteger * result;
for (NSInteger * hour in totalhours)
{
result += hour;
}
NSIntegeris not a class, it’s atypedefforint. You cannot put it into collections likeNSArraydirectly.You need to wrap your basic data types (
int,char,BOOL,NSInteger(which expands toint)) intoNSNumberobjects to put them into collections.NSIntegerdoes work with+=, keep in mind that your code uses pointers to them, which is probably not what you want anyway here.So
would work.
If you put them with
[NSNumber numberWitInt:a];etc. into anNSArray, this is not that easy and you need to use-intValuemethods to extract their values first.