Objective-C += equivalent? - iphone

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;
}

NSInteger is not a class, it's a typedef for int. You cannot put it into collections like NSArray directly.
You need to wrap your basic data types (int, char, BOOL, NSInteger (which expands to int)) into NSNumber objects to put them into collections.
NSInteger does work with +=, keep in mind that your code uses pointers to them, which is probably not what you want anyway here.
So
NSInteger a = 1, b = 2;
a += b;
would work.
If you put them with [NSNumber numberWitInt:a]; etc. into an NSArray, this is not that easy and you need to use -intValue methods to extract their values first.

If totalhours actually contains NSNumber objects you need the following:
NSInteger result = 0;
for(NSNumber* n in totalhours)
{
result += [n integerValue];
}

The problem is that you are confusing NSInteger (a typedef for int or long) with a class instance such as NSNumber.
If your totalhours object is an array of NSNumber objects, you'll need to do:
NSInteger result;
for (NSNumber *hour in totalhours)
{
result += [hour integerValue];
}

No problem using the '+=' operator, just be sure about the objects you are working with...
Your code might be :
NSNumber *n; NSUInteger t = 0;
for(n in totalHours) {
t += [n integerValue];
}
// you got your total in t...

The += operation definitly works. All you need to do is initialize your result variable so it has a start value.
E.g. NSInteger * result = 0;
Good luck!

Your problem is probably that you're using a pointer to an NSInteger instead of an actual NSInteger. You're also not initializing it. Try this:
NSInteger result = 0;
for (NSInteger * hour in totalhours)
{
result += *hour;
}

Related

Divide NSInteger with int

I am trying to divide NSInteger with some number, but I am receiving errors.
This is what I am trying to do:
length = [corner count];
for (int i=0; i<length; i++) {
if ([resultDate rangeOfString:[corner objectAtIndex:i]].location != NSNotFound) {
cornerResult++;
}
}
cornerResultLabel.text = [NSString stringWithFormat:#"%i", cornerResult];
This works as it should.. I search through the array and count the results and write it.
But, I need cornerResult divided with 4. When I add cornerResult / 4 it shows me the error(as I wrote in comment). I have no idea why is this making problem.
you probably meant to divide the intValue of the NSNumber, not the NSNumber* itself:
int num = number.intValue;
int result = num / 4;
(full code sample and error message would help)
You are dividing an int which becomes a float , use %f instead.
I fix it this way. First I defined int and after that I set NSInteger value to int and after that divide.
int helper = cornerResult;
helper = helper / 4;

Store/Retrieve values from Objective-C++ to Objective-C

How can I store an Objective-C++ short int like the one below in an Objective-C array and convert it back to Objective-C++ later? I've attempted the following with no success. Any help would be great!
short int *tmpbuffer = ( short int*)malloc(sizeof(short int)*length*inData);
int count = 0;
for (count = 0; count < length*inData; count++)
{
tmpbuffer[count] = (short int) (inbuffer[count] * 0x7fff);
}
size = fwrite(tmpbuffer, 1, sizeof(short int)*length*inData,fp);
[fwriteSections addObject:[NSNumber numberWithShort:tmpbuffer]];
[fwriteSections addObject:[NSNumber numberWithShort:sizeof(short int)*length*inchannels]];
[fwriteRows addObject:fwriteSections];
There is no need to do any conversion between Objective C++ and Objective C for a simple byte buffer. You can just pass a short int pointer between Objective C++ and Objective C classes.
If you mean, how can you convert a short int byte buffer to an NSArray, then you are on the right track, just do the following:
short int *buffer = malloc(size * sizeof(short int));
NSMutableArray *shortArray = [NSMutableArray arrayWithCapacity:size];
for (NSUInteger i = 0; i < size; i++) {
[shortArray addObject:[NSNumber numberWithShort:buffer[i]]];
}
I would not recommend this approach though, it is not efficient, you are better off just sticking with the C style buffer.

Is it possible to reference a variable with a string and an int?

Is it possible to reference a variable with a string and an int, like this:
int number1;
int j = 1;
#"number%i", j = 3; //Hope this makes sense..
The code above gives me warnings and does not work, how could this be done.
I also tried this, but it doesnt work (for quite obvious reasons):
int j = 1;
NSString *refString = [NSString stringWithFormat:#"number%i", j];
refString = 3;
Im really struggling with this, I know how to do it in Javascript, but not in Obj-C, is it possible?
This is an anti-pattern I call the Poor Man's Array. The better way to do this is to use a proper collection like an array instead of a bunch of variables that are secretly related. Done right, the code with an array will usually be a lot shorter and cleaner too.
From what I can infer you are trying to set/retrieve different variables based on the value of j.
You could use a dictionary for this purpose:
NSMutableDictionary *numbers = [NSMutableDictionary dictionary];
int j = 1;
[numbers setObject:[NSNumber numberWithInt:3] forKey:[NSNumber numberWithInt:j]];
And then to retrieve:
[[numbers objectForKey:[NSNumber numberWithInt:j]] intValue];
It's a bit verbose, but you could simplify it by creating a small class.

use if with NSinteger?

I WANT to use NSinteger variable *strength in my code with if condition but it's not work.. :(
if(strength == 11){
}
How can i use if with NSInteger*
NSInteger is a primitive value type; you don't really need to use pointers. So your declaration should read
NSInteger strength;
And not
NSInteger *strength;
However if you do need to use a pointer to an NSInteger (that is, NSInteger *) for some reason, then you need to dereference the pointer to get the value:
if (*strength == 11) {
}
but from what I see, I don't think this is the case.
I assume you must be adding an * when you declare your strength variable. You shouldn't have it because NSInteger is a primitive type.
Why don't I declare NSInteger with a *

iPhone maths not quite adding up right

I am adding values held within an array but the sum is +1 what it actually should be.
//update totalscore
uint newTotalScore;
for (uint i=0; i< [bestscoresArray count] ; i++) {
newTotalScore += [[bestscoresArray objectAtIndex:i] intValue];
}
totalscore = newTotalScore;
//output
l1bestscore=15900, l2bestscore=7800, l3bestscore=81000, l4bestscore=81000, l5bestscore=0, l6bestscore=0, l7bestscore=0, l8bestscore=0, l9bestscore=0, l10bestscore=0, totalscore=185701
As you can see the totalscore output is 185701 but the sum of all values is 185700.
Would anyone have any ideas why this is occurring?
Thanks,
Mark
You must define newTotalScore's initial value:
uint newTotalScore = 0;
Otherwise it will be undefined. In your case it was 1 but it could have been any other value.
Not sure about this, but did you try initializing newTotalScore to zero? (See this question about variable initialization.) If that does not help, give us more code.