Objective-C loop through array
16,000
Solution 1
You are NSLog
ing the whole array, not the current index of array1. What you are seeing logged is what you've coded - to log what you are expecting, change NSLog(@"%@",array1);
to NSLog(@"%@",[array1 objectAtIndex:x]);
To confirm add the following after your assignment loop:
for (NSObject* o in array1)
{
NSLog(@"%@",o);
}
Solution 2
Use NSLog(@"%@", [array1 objectATIndex:x]);
if (count == 0)
{
[array1 addObject:@"No Items"];
}
else
{
int x;
for (x = 0;x <= count; x++)
{
[array1 addObject:[itemsArray objectAtIndex:x];
NSLog(@"%@", [array1 objectATIndex:x]);
}
}
Author by
provdr
Updated on June 04, 2022Comments
-
provdr almost 2 years
I have code similar to this
if (count == 0) { [array1 addObject:@"No Items"]; } else { int x; for (x = 0;x <= count; x++) { [array1 addObject:[itemsArray objectAtIndex:x]; NSLog(@"%@",array1); } }
itemsArray
has numbers in it (0-40). My expected result is:- 1
- 2
- 3
- ...
However it actually does:
- 1
- 1,2
- 1,2,3
- 1,2,3,4
- 1,2,3,4,5
- ...
Why does this happen? If possible, I'd also like to ask for an example to use fast enumeration for this situation (if it suits for this).
Thanks in advance.