I can’t understand what is happening in my code:
for (NSMutableDictionary *dict in jsonResponse) {
NSString *days = [dict objectForKey:@"dayOfTheWeek"];
NSArray *arrayDays = [days componentsSeparatedByString:@" "];
NSLog(@"la var %@ size %lu", days, sizeof(arrayDays));
for(int i = 0; i<sizeof(arrayDays); i++){
NSLog(@"el dia %@",[arrayDays objectAtIndex:i]);
}
}
What I get in log:
2012-07-02 10:06:57.191 usualBike[1342:f803] var M T W T F size 4
2012-07-02 10:06:57.191 usualBike[1342:f803] day M
2012-07-02 10:06:57.192 usualBike[1342:f803] day T
2012-07-02 10:06:57.192 usualBike[1342:f803] day W
2012-07-02 10:06:57.193 usualBike[1342:f803] day T
2012-07-02 10:06:57.193 usualBike[1342:f803] var S S size 4
2012-07-02 10:06:57.194 usualBike[1342:f803] day S
2012-07-02 10:06:57.194 usualBike[1342:f803] day S
And crashes, because position 3 doesn’t exist.
Why the size is not changing the second time? it should be 1.
Thank you in advance
sizeof(theArray)calculates the size of the datatype, see Wikipedia.[theArray count]andtheArray.Countreturn the count of objects in the array.Replace both occurrences of
sizeof(arrayDays)byarrayDays.Count.Note: You can also loop the array using the following: