int++ increments by 4 - iphone

Here's my code:
- (IBAction)NextTouched:(id)sender {
NSLog(#"Index = %i", index);
if([project getCount]>(index++)) {
[self setUI:index];
}
}
Index is an integer, as declared in my .h file:
#property (nonatomic) int *index;
But every time I click the button, the log says the integer is going up by 4. Can you tell my why?

The reason it's going up by 4 is because index is a pointer. When you increment a pointer its value increases by the size of the data type it points to, in this case an int, which is 4 bytes.
Given index appears to be an index into an NSArray (or some other collection class), I think you want to make it int and not int * to solve your issue. Better still make it unsigned, like NSUInteger, which is the type returned from the count method.
Also I think you'll want to use prefix-increment rather than postfix-increment so that the if test uses the newly incremented value, not the previous value.

Simply define index as an integer variable rather than pointer and if you want to print the value before increment use index++ else use ++index to increment the value and then print

Related

IndexOfObject return 2147483647

I have an array with 10 items. When I call "IndexOfObject" for the elements number 9 and the element number 10 Xcode return an exception: "NSRangeException"
reason: '_[_NSCFArray objectAtIndex:] index:2147483647 beyond
bounds(10)'.
From a previous NSLog, I saw that the two elements exist in the array but indexOfObject not find them. Why?
My code is:
NSDictionary * headConfig =[avatarDictionaryToSave objectForKey:#"head_dictionary"];
NSString * headImage =[headConfig objectForKey:#"layer_key"];
NSString * pathFace =[[NSBundle mainBundle]pathForResource:#"Face" ofType:#"plist"];
NSLog(#"%#", headImage);
NSArray *arrayFace =[NSArray arrayWithContentsOfFile:pathFace];
NSLog(#"the elements are: %#", arrayFace);//here headImage is present
int index =[arrayFace indexOfObject:headImage];
NSLog(#"the index is %d", index);
indexOfObject: returns NSNotFound when the object is not present in the array. NSNotFound is defined as NSIntegerMax (== 2147483647 on iOS 32 bit).
So it seems that the object you are looking for is just not there.
Please change the coding of adding the array values as I mentioned below.
// [arr_values addObject:[dictionary valueForKey:#"Name"]];
[arr_values addObject:[NSString stringWithFormat:#"%#",[dictionary valueForKey:#"Name"]]];
When target array elements are not in string format then while we using the
indexOfObject then that value can't able to find in the target array. So please try to
change the value of object as mentioned above while adding into array.
By default, an integer is assumed to be signed.
In other words the compiler assumes that an integer variable will be called upon to store either a negative or positive number. This limits the extent that the range can reach in either direction.
For example, a 32-bit int has a range of 4,294,967,295. In practice, because the value could be positive or negative the range is actually −2,147,483,648 to +2,147,483,647.
If we know that a variable will never be called upon to store a negative value, we can declare it as unsigned, thereby extending the (positive) range to 0 to +4,294,967,295.
An unsigned int is specified as follows:
unsigned int myint;

read int dynamically in array

I have values i wanted to load dynamically in an array, here are the examples
First of all i define 3 different values in my init, and then in my array i want it to determine which value to read. Example:
First i define the value
int value1=20
int value2=40;
int value3=60;
i then define another int in my array called valueToLoad, and i'll give each of them a number tag. and i want the individual array item to read different value based on their number tag so that Item 1 will read value1, Item 2 will read value2 and so on. i tried the method below to convert NSString into int:
NSString *valueVariable=[NSString stringWithFormat:#"value%d",i]; (i being the number tag)
int valueToRead = [valueVariable intValue];
unfortunately, this conversion doesn't supports conversion of any other thing except if the string is actual integer.
However i do not want to run the IF statement to do:
if(tag==1)
{ int valueToLoad= value1;}
For who don't understand. I am just trying to read different Int value in an array based on the number of array. Let's assume i have 3 Items in array naming A,B,and C. i want Item A to read Value 1, ItemB to read Value2 and so on.
Why don't you simply do something like
int values[] = {20,40,60};
...
int valueToRead = values[i]; //or i-1, depending if i starts from 0 or 1
?
Not sure about the context of your problem but why don't you use an NSDictionary?
That way you can store your number tag as the key and your value to read as the value...
You fill your dictionary like this:
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:value1, tag1, value2, tag2, nil];
Read your value using : [dict objectForKey:tag1];
You can use an array, store your variables in there, and index into it.
You can use a dictionary, store your variables as values, and look them up by key.
You can declare all your variables as #property, and then use [self valueForKey:] to look them up by name.
You can build the name of the ivar as a string, and then use something like object_getInstanceVariable() to retrieve it's value directly (this is similar to #3, except you don't have to declare it as an #property).
If you're dealing with views, you can assign each view a unique tag and then retrieve it via [superview viewWithTag:aTag].
EDIT: Note that this only works with instance variables. This does not work with global/static variables.
took from: Objective C Equivalent of PHP's "Variable Variables"

Getting Index of an Object from NSArray?

i am trying to get index of an array through indexOfObject method as follows but when i try to log the value to test the index i get a garbage value.. for testing purposes i am having an array with values {57,56,58..} to get an index of lets say 56,
NSNumber *num = [NSNumber numberWithInteger:56];
NSInteger Aindex = [myArray indexOfObject:num];
NSLog(#" %d",Aindex);
the value i get is something like 2323421. what am i possibly doing wrong??
The index returned by indexOfObject will be the first index for an occurence of your object. Equality is tested using isEqual method.
The garbage value you get is probably equal to NSNotFound.
Try testing anIndex against it. The number you are looking for isn't probably in your array :
NSNumber *num=[NSNumber numberWithInteger:56];
NSInteger anIndex=[myArray indexOfObject:num];
if(NSNotFound == anIndex) {
NSLog(#"not found");
}
or log the content of the array to be sure :
NSLog(#"%#", myArray);
Folks,
When an object is not found in the array the indexOfObject method does NOT return a 'garbage' value. Many systems return an index of -1 if the item is not found.
However, on IOS - because the indexOfObject returns an UNSIGNED int (aka NSUInteger) the returned index must be greater than or equal to zero. Since 'zero' is a valid index there is no way to indicate to the caller that the object was not found -- except by returning an agreed upon constant value that we all can test upon. This constant agreed upon value is called NSNotFound.
The method:
- (NSUInteger)indexOfObject:(id)anObject;
will return NSNotFound if the object was not in the array. NSNotFound is a very large POSITIVE integer (usually 1 minus the maximum int on the platform).
NSNumber *num1 = [NSNumber numberWithInt:56];
NSNumber *num2 = [NSNumber numberWithInt:57];
NSNumber *num3 = [NSNumber numberWithInt:58];
NSMutableArray *myArray = [NSMutableArray arrayWithObjects:num1,num2,num3,nil];
NSNumber *num=[NSNumber numberWithInteger:58];
NSInteger Aindex=[myArray indexOfObject:num];
NSLog(#" %d",Aindex);
Its giving the correct output, may be u have done something wrong with storing objects in ur array.
Try this:
NSArray's indexOfObject: method. Such as the following:
NSUInteger fooIndex = [someArray indexOfObject: someObject];
If you're using Swift and optionals make sure they are unwrapped. You cannot search the index of objects that are optionals.
I just checked. Its working fine for me. Check if your array has the particular number. It will return such garbage values if element is not present.
indexOfObject methord will get the index of the corresponding string in that array if the string is like #"Test" and you find like #"TEST" Now this will retun an index like a long number

int and ++new goes up by 2 every time

Just a silly question:
I have a simple counter, but it seems that it gives the double value of what I expect.
short int *new = 0;
++new;
NSLog(#"Items: %hi", new);
And this returns:
Items: 2
Relatively new to Cocoa, and still working out the little details as is clear form above...
You don't have an integer variable, you have a pointer to an integer variable (a short integer, to be specific). It increments by 2 because short integers are two bytes long. A pointer variable holds a memory address of another value. Incrementing a pointer means, "make this pointer point to the next thing in memory", where "thing" is the type of value the pointer was declared to point at. A pointer to double would increment by 8 each time.
The "*" in the declaration makes it a pointer. If you just want an int, you'd just write
short int new = 0;
++new;
Aah, when you increment a pointer, in increments it by the size of the object it holds. You're looking at an address, not a number.
do this, and see:
short int *new = 0;
NSLog(#"Items now: %hi", new);
++new;
NSLog(#"Items then: %hi", new);
Because the way you define new is as a pointer to an integer, *new. You set the memory location to contain a short int, which is a 16-bit integer, so it takes up two bytes in memory. So increasing that on the second line means increasing the memory location by 2.
I don't think you intend to deal with memory locations. It's kind of odd to define an integer and also control its location in memory, unless in specific situations. Code that would do what you want is:
short int new = 0;
++new;
NSLog(#"Items: %hi", new);

NSNumber LoopVar++ in for loop increments by 4 instead of by 1

I'm quite sure that this question has a very simple answer that I should have figured out by now. Since I haven't yet done so I come to you, stack overflow hive mind.
I expected the loop below to print out the numbers from 0 to 5. Instead it prints out only 0 and 4. Why does LoopNumber++ increment my NSNumber LoopNumber by 4 instead of by 1?
NSNumber *LoopNumber;
for (LoopNumber=0; LoopNumber<=5; LoopNumber++) {
NSLog(#"%d",LoopNumber);
}
If I change it to the following it works exactly as I expect. What gives?
for (int LoopNumber=0; LoopNumber<=5; LoopNumber++) {
I'm fooling around with an iPhone project in XCode 3.2.1, using SDK 3.1.2.
an NSNumber is not an integer. It is an object wrapper for a number which may be an integer.
The variable LoopNumber is actually a pointer to the location in memory where the object should be. All LoopNumber itself holds is a memory address, which on your machine is 4 bytes long. When you do LoopNumber++ you are inzoking pointer aritmatic on the pointer and it is advancing to the next memory address which is four bytes later. You can see this by doing a sizeof(LoopNumber) - that would return 4 on your system.
What you really want to do is use a regular integer like so:
NSUInteger loopNumber;
for(loopNumber = 0; loopNumber <= 5; loopNumber++) {
NSLog(#"%d",loopnumber);
}
or if you really need to use NSNumbers:
NSNumber loopNumber;
for(loopNumber = [NSNumber numberWithInt:0]; [loopNumber intValue] <= 5; loopNumber=[NSNumber numberWithInt:[loopNumber intValue]++]) {
NSLog(#"%d",loopnumber);
}
int is native type. NSNumber is a Objective-C class. Use float or int when doing real work. But to put a int into a collection you can create an NSNumber object from the int or float native type.