Divide NSInteger with int - iphone

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;

Related

get random string with limit 10 maximum ios?

am generating random string it contains alpha numberic values. The thing is i need to set exactly 10 digits to store in a particular string sometime i getting 10 digits exactly but most of the time i am getting 4,5,7, or even 1 character values :
here my sample code :
NSString *alphabet = #"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXZY0123456789";
NSMutableString *s = [NSMutableString stringWithCapacity:10];
for (NSUInteger i = 0; i < 10; i++) {
u_int32_t r = arc4random() % [alphabet length];
unichar c = [alphabet characterAtIndex:r];
[s appendFormat:#"%C", c];
NSLog(#"%#",s);
}
my nslog :
y
yC
yCD
yCDC
yCDCd
yCDCdP
yCDCdP1
yCDCdP1F
yCDCdP1Fg
yCDCdP1Fgq
There's nothing wrong with the code.
Just, put NSLog(#"%#",s); right out of the for loop.
You have put your NSLog inside the for loop. That might make you think it's wrong. Take
NSLog(#"%#",s);
out of the loop.

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.

Objective-C += equivalent?

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

How to convert a double to NSInteger?

Very simple question here. I have a double that I wish to convert back to a NSInteger, truncating to the units place. How would I do that?
Truncation is an implicit conversion:
NSInteger theInteger = theDouble;
That's assuming you're not checking the value is within NSInteger's range. If you want to do that, you'll have to add some branching:
NSInteger theInteger = 0;
if (theDouble > NSIntegerMax) {
// ...
} else if (theDouble < NSIntegerMin) {
// ...
} else {
theInteger = theDouble;
}
NSInteger is a typedef for a C type. So you can just do:
double originalNumber;
NSInteger integerNumber = (NSInteger)originalNumber;
Which, per the C spec, will truncate originalNumber.
but anyway, assuming you want no rounding, i believe this should work simply
double myDouble = 10.4223;
NSInteger myInt = myDouble;
edit for rounding: (i'm sure theres a much simpler (and precise) way to do this.. (this also doesn't account for negative numbers or maximum boundaries)
double myDecimal = myDouble - myInt;
if(myDecimal < 0.50)
{
//do nothing
}
else
{
myInt = myInt + 1;
}
NSInteger is a typedef, it's the same as using an int. Just assign the value like:
double d;
NSInteger i = d;
JesseNaugher mentions rounding and I note the OP needs were met with a simple truncate, but in the spirit of full generalisation it's worth remembering the simple trick of adding 0.5 to the double before invoking floor() to achieve rounding. Extending Jonathan Grynspan's comment: NSInteger myInt = floor(myDouble + 0.5); i.e., rounding up in absolute terms. If rounding 'up' means rounding away from zero a more convoluted approach is needed: NSInteger myInt = floor( myDouble + (myDouble < 0.0 ? -0.5 : 0.5) );

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.